设置“Me.Labelnn.BackColor”在具有许多标签的报告中使用 for nn=... 循环
我有一份报告,就像一整周的每小时时间表。计划中的每个单元格为 1 台机器 1 天。每列是 7 台机器之一。因此,一行由 7 天的单元格组成。到目前为止一切顺利,但每天需要 18 行文本(一个备注字段)并且 - 每行都需要着色。我为 7 列中的每一列创建了 18 个编号为 Label33 及以上的标签。
我用相当大的 case 语句设置他们的 BackColor,其中有 7 个案例,每个案例有 18 x Labelnn.Backcolor=aColor 。
但是,我想用形成 control_name="Label" & 的内容替换它。 n
n,然后执行类似 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" & n
n, 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Cody Gray 是正确的,但如果您不知道什么是控制数组,那么这没有任何帮助。
以下是 Excel VBA,但我对 Access VBA 也做过同样的事情,但最近没有。我认为语法是相同的,但我不保证它。
我创建了一个工作簿,插入了一个表单,将选定的控件拖到其上,然后运行以下代码:
立即窗口的输出是:
您可以看到
Label1.xxx
与.Controls(0).xxx
。我系统地命名我的控件,以便可以运行如下代码:当我不知道需要多少特定类型的控件时,我最常使用此功能。比如说,我创建了 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:
The output to the immediate window was:
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: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.
我同意科迪·格雷的观点。使用控制数组。
这是一个可以帮助您的链接
http:// /www.siddharthroout.com/index.php/2018/01/15/vba-control-arrays/
虽然托尼为您提供了一种相对简单的方法,但在某些情况下您可能会添加或删除一个控件,或者只是搞乱顺序。另外,在某些情况下,您根本无法将标签保留为
Label1
。您可能会向其中添加一些文本,以便它表示某些内容...在这种情况下,请使用
TypeOf
而不是Name
例如
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 ofName
For example