设置“Me.Labelnn.BackColor”在具有许多标签的报告中使用 for nn=... 循环

发布于 2024-12-25 21:58:03 字数 423 浏览 0 评论 0原文

我有一份报告,就像一整周的每小时时间表。计划中的每个单元格为 1 台机器 1 天。每列是 7 台机器之一。因此,一行由 7 天的单元格组成。到目前为止一切顺利,但每天需要 18 行文本(一个备注字段)并且 - 每行都需要着色。我为 7 列中的每一列创建了 18 个编号为 Label33 及以上的标签。

我用相当大的 case 语句设置他们的 BackColor,其中有 7 个案例,每个案例有 18 x Labelnn.Backcolor=aColor 。

但是,我想用形成 control_name="Label" & 的内容替换它。 nn,然后执行类似 Me.control_name.BackColor=aColor 的操作。

可能的?如何? (不,考虑到 VBA-Access 的灵活性,我还没有尝试过上述方法。)

I have a report which is like an hourly schedule for a whole week. Each cell in the schedule is 1 day for 1 machine. Each column is one of 7 machines. Thus, a row consists of 7 day cells. So far so good, but each day needs 18 rows of text (a Memo field) and - each row needs to be colorized. I've created 18 labels numbered Label33 and up, for each of the 7 columns.

I'm setting their BackColor with a pretty large case statement of 7 cases and 18 x Labelnn.Backcolor=aColor in each case.

However, I'd like to replace this with something that forms the control_name="Label" & nn, and do something like Me.control_name.BackColor=aColor.

Possible? How? (No, considering how flexible VBA-Access is I haven't tried the above yet.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红墙和绿瓦 2025-01-01 21:58:03

Cody Gray 是正确的,但如果您不知道什么是控制数组,那么这没有任何帮助。

以下是 Excel VBA,但我对 Access VBA 也做过同样的事情,但最近没有。我认为语法是相同的,但我不保证它。

我创建了一个工作簿,插入了一个表单,将选定的控件拖到其上,然后运行以下代码:

Option Explicit
Sub TestControls()

  Dim InxC As Long

  Load UserForm1

  With UserForm1
    For InxC = 0 To .Controls.Count - 1
      Debug.Print .Controls(InxC).Name
    Next
  End With

End Sub

立即窗口的输出是:

Label1
CommandButton1
ComboBox1
CommandButton2
OptionButton1

您可以看到 Label1.xxx.Controls(0).xxx。我系统地命名我的控件,以便可以运行如下代码:

  With UserForm1
    For InxC = 0 To .Controls.Count - 1
      If Mid(.Controls(InxC).Name,1,5) = "lblXx" Then
        ' Code to set properties of all lblXx controls
      End If
    Next
  End With

当我不知道需要多少特定类型的控件时,我最常使用此功能。比如说,我创建了 10 个,这超出了我的需要,并且让它们全部隐形。

在运行时,我使那些我需要的可见,并根据需要设置它们的顶部和左侧属性。

Cody Gray is correct but this is not helpful if you do not know what a control array is.

The following is Excel VBA but I have done the same with Access VBA but not recently. I think the syntax is the same but I do not guarantee it.

I created a workbook, inserted a form, pulled a selection of controls onto it and then ran the following code:

Option Explicit
Sub TestControls()

  Dim InxC As Long

  Load UserForm1

  With UserForm1
    For InxC = 0 To .Controls.Count - 1
      Debug.Print .Controls(InxC).Name
    Next
  End With

End Sub

The output to the immediate window was:

Label1
CommandButton1
ComboBox1
CommandButton2
OptionButton1

You can see that Label1.xxx is exactly the same as .Controls(0).xxx. I name my controls systematically so I can run code like:

  With UserForm1
    For InxC = 0 To .Controls.Count - 1
      If Mid(.Controls(InxC).Name,1,5) = "lblXx" Then
        ' Code to set properties of all lblXx controls
      End If
    Next
  End With

I use this functionality most when I do not know how many of a particularly type of control I need. I create 10, say, of them which is more than I will ever need and make them all invisible.

At run time, I make those I need visible and set their top and left properties as necessary.

北恋 2025-01-01 21:58:03

我同意科迪·格雷的观点。使用控制数组

这是一个可以帮助您的链接

http:// /www.siddharthroout.com/index.php/2018/01/15/vba-control-arrays/

虽然托尼为您提供了一种相对简单的方法,但在某些情况下您可能会添加或删除一个控件,或者只是搞乱顺序。另外,在某些情况下,您根本无法将标签保留为 Label1。您可能会向其中添加一些文本,以便它表示某些内容...

在这种情况下,请使用 TypeOf 而不是 Name

例如

Option Compare Database

Private Sub Command1_Click()
    Dim ctl As Control
    For Each ctl In Me.Controls
        If TypeOf ctl Is Label Then
            Debug.Print ctl.Name
        End If
    Next ctl
End Sub

I agree with Cody Gray. Use the Control Arrays.

Here is a link which will help you

http://www.siddharthrout.com/index.php/2018/01/15/vba-control-arrays/

Though Tony has given you a relatively easy way to do it but there are instances where you might add or delete a control or simply mess up on the sequence. Also there are instances when you simply cannot leave the label as Label1. You might be adding some text to it so that it signifies something...

In such a scenario use TypeOf instead of Name

For example

Option Compare Database

Private Sub Command1_Click()
    Dim ctl As Control
    For Each ctl In Me.Controls
        If TypeOf ctl Is Label Then
            Debug.Print ctl.Name
        End If
    Next ctl
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文