循环控制时设置控制 属性
Set Control Property While Looping Through Controls
下面的 VBA 代码可用于遍历窗体上的所有控件,但如何使用控件名称 c.name 设置控件的任何属性?
Dim c As Control
For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
MsgBox "Control Name= " & c.name & " Control Value = " & c.Value
'I'm looking for this Part: c.name .BackColor = 255
End If
Next
呃,设置属性?
Dim c As Control
For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
c.BackColor = 255
End If
Next
Intellisense 将无法工作,因为您使用的是一般 Control
而不是特定的 TextBox
界面,但它会 运行 很好。
太久没有涉足VBA了。
我的问题的(明显的)正确答案是:
c.BackColor = 255
要设置 Textbox
的 BackColor
,您可以直接访问它:TextBox1.BackColor = 255
.
您尝试这样做的方式是通过 TextBox
的 Name
属性,它只是 returns 一个字符串。
本质上你所做的是 TextBox1.Name.BackColor = 255
,这会导致错误:
compile error: invalid qualifier.
所以正确的方法应该是这样的:
Dim c As Control
For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
c.BackColor = 255
End If
Next
如果您想使用智能感知,那么您可以设置对临时变量的引用。
Dim c As Control
For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
Dim TempTextbox As Access.TextBox
Set TempTextbox = c
TempTextbox.BackColor = 255
End If
Next
这是文本框对象 (Access) 上的 Microsofts Documentation。您可以将其用作其不同方法和属性的参考。
最好先定义要更改的控件,然后将其包含在 For 循环中。
Dim c As Control
Dim txt As TextBox
For Each c In Me.Controls
If TypeOf c Is TextBox Then
Set txt = c
txt.BackColor = RGB(255, 255, 255)
End If
Next c
下面的 VBA 代码可用于遍历窗体上的所有控件,但如何使用控件名称 c.name 设置控件的任何属性?
Dim c As Control
For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
MsgBox "Control Name= " & c.name & " Control Value = " & c.Value
'I'm looking for this Part: c.name .BackColor = 255
End If
Next
呃,设置属性?
Dim c As Control
For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
c.BackColor = 255
End If
Next
Intellisense 将无法工作,因为您使用的是一般 Control
而不是特定的 TextBox
界面,但它会 运行 很好。
太久没有涉足VBA了。 我的问题的(明显的)正确答案是: c.BackColor = 255
要设置 Textbox
的 BackColor
,您可以直接访问它:TextBox1.BackColor = 255
.
您尝试这样做的方式是通过 TextBox
的 Name
属性,它只是 returns 一个字符串。
本质上你所做的是 TextBox1.Name.BackColor = 255
,这会导致错误:
compile error: invalid qualifier.
所以正确的方法应该是这样的:
Dim c As Control
For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
c.BackColor = 255
End If
Next
如果您想使用智能感知,那么您可以设置对临时变量的引用。
Dim c As Control
For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
Dim TempTextbox As Access.TextBox
Set TempTextbox = c
TempTextbox.BackColor = 255
End If
Next
这是文本框对象 (Access) 上的 Microsofts Documentation。您可以将其用作其不同方法和属性的参考。
最好先定义要更改的控件,然后将其包含在 For 循环中。
Dim c As Control
Dim txt As TextBox
For Each c In Me.Controls
If TypeOf c Is TextBox Then
Set txt = c
txt.BackColor = RGB(255, 255, 255)
End If
Next c