vba 如何在代码中访问控件的标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1637083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access the label of a control in code
提问by Markus
Often a label control is bound to a textbox or similar other control. I know that there is a way to access the label through code, but I do not remember how to do it.
通常,标签控件绑定到文本框或类似的其他控件。我知道有一种方法可以通过代码访问标签,但我不记得怎么做。
Can anyone help?
任何人都可以帮忙吗?
Maybe something like:
也许是这样的:
Me.txtName.Child!lblName.Value
Or
或者
Me.txtName.Parent!lblName.Value
I will use this when I am enumerating through the controls on a form for validation purposes. Then I want to use the label for a control as information in the error message to show the user where the error is.
当我枚举表单上的控件以进行验证时,我将使用它。然后我想使用控件的标签作为错误消息中的信息来向用户显示错误所在。
回答by Adriaan Stander
With the textbox you can try
使用文本框,您可以尝试
Text0.Controls.Item(0).Caption
where Control 0 is the linked label
其中 Control 0 是链接标签
回答by David-W-Fenton
@Astander has provided the correct answer, but keep in mind that not all controls have the same kind of Controls collections.
@Astander 提供了正确答案,但请记住,并非所有控件都具有相同类型的 Controls 集合。
TextBoxes, ComboBoxes, ListBoxes, CheckBoxes have a maximum of 1 item in their controls collection (the attached label), but if the label isn't attached, they won't even have that, so .Controls(0) will throw an error.
TextBoxes、ComboBoxes、ListBoxes、CheckBoxes 在它们的控件集合中最多有 1 个项目(附加的标签),但如果没有附加标签,它们甚至不会有,所以 .Controls(0) 会抛出错误.
An Option Group has multiple controls, the label and the option button or toggle buttons inside the frame. When you drop an option group on a form from the form tools toolbar, the frame is created with an attached label, so it will be the control with index 0. But if, for instance, you delete the default label, add option buttons and then add back a label, it will not be index 0, but index .Controls.Count - 1.
一个选项组有多个控件、标签和框架内的选项按钮或切换按钮。当您从表单工具工具栏将选项组拖放到表单上时,会创建带有附加标签的框架,因此它将是索引为 0 的控件。但是,例如,如果您删除默认标签,则添加选项按钮和然后再添加一个标签,它不会是索引 0,而是索引 .Controls.Count - 1。
So, for the caption of an option group lable, you either want to be careful that if you delete the default label, you also delete the controls inside the frame after you add the label back. If that's not the case, you need to name the label and refer to it by name, because the labels for the option/toggle buttons are part of the option group's Controls collection (this surprised me -- I expected them be only in the Controls collection of the option/toggle button to which they were attached).
因此,对于选项组标签的标题,您要么要小心,如果删除默认标签,则在重新添加标签后也会删除框架内的控件。如果不是这种情况,您需要命名标签并按名称引用它,因为选项/切换按钮的标签是选项组的 Controls 集合的一部分(这让我感到惊讶——我希望它们只出现在 Controls它们附加到的选项/切换按钮的集合)。
To avoid this problem, I can imagine convoluted code where you looped through the option group's Controls collection looking for the labels attached to the option/toggle buttons, and then looped through the option group's Controls collection a second time, this time looking only at the labels. Something like this:
为了避免这个问题,我可以想象一下复杂的代码,您在选项组的 Controls 集合中循环查找附加到选项/切换按钮的标签,然后第二次在选项组的 Controls 集合中循环,这次只查看标签。像这样的东西:
Public Function FindOptionGroupLabel(ctlOptionGroup As Control) As Control
Dim ctl As Control
Dim strOptionToggleLabels As String
If ctlOptionGroup.ControlType <> acOptionGroup Then
MsgBox ctlOptionGroup.Name & " is not an option group!", _
vbExclamation, "Not an option group"
Exit Function
End If
For Each ctl In ctlOptionGroup.Controls
Select Case ctl.ControlType
Case acOptionButton, acToggleButton
If ctl.Controls.Count = 1 Then
strOptionToggleLabels = strOptionToggleLabels & " " & ctl.Controls(0).Name
End If
End Select
Next ctl
strOptionToggleLabels = strOptionToggleLabels & " "
For Each ctl In ctlOptionGroup.Controls
Select Case ctl.ControlType
Case acLabel
If InStr(" " & strOptionToggleLabels & " ", ctl.Name) = 0 Then
Set FindOptionGroupLabel = ctl
End If
End Select
Next ctl
Set ctl = Nothing
End Function
Now, this breaks if there is no label attached, so it would probably make more sense for it to return the label name, rather than the control reference:
现在,如果没有附加标签,这会中断,因此返回标签名称而不是控件引用可能更有意义:
Public Function FindOptionGroupLabel(ctlOptionGroup As Control) As String
Dim ctl As Control
Dim strOptionToggleLabels As String
If ctlOptionGroup.ControlType <> acOptionGroup Then
MsgBox ctlOptionGroup.Name & " is not an option group!", _
vbExclamation, "Not an option group"
Exit Function
End If
For Each ctl In ctlOptionGroup.Controls
Select Case ctl.ControlType
Case acOptionButton, acToggleButton
If ctl.Controls.Count = 1 Then
strOptionToggleLabels = strOptionToggleLabels & " " & ctl.Controls(0).Name
End If
End Select
Next ctl
strOptionToggleLabels = strOptionToggleLabels & " "
For Each ctl In ctlOptionGroup.Controls
Select Case ctl.ControlType
Case acLabel
If InStr(" " & strOptionToggleLabels & " ", ctl.Name) = 0 Then
FindOptionGroupLabel = ctl.Name
End If
End Select
Next ctl
Set ctl = Nothing
End Function
This could probably be done with a single loop through the option group's Controls collection, but it's late! What's there seems pretty close to bullet-proof, not that anyone gives a rat's ass, of course! :)
这可能可以通过选项组的 Controls 集合的单个循环来完成,但已经晚了!那里的东西似乎非常接近防弹,当然不是有人给老鼠屁股!:)
回答by Perry Sugerman
Here is some code that I wrote to rename the labels associated with OptionButtons. A label has a parent property that points to the control it labels. The function is pretty general while the subroutine is written for OptionButtons. This code should work for almost anything except when the label is not associated, I have not provided any recovery for that.
这是我编写的一些代码,用于重命名与 OptionButtons 关联的标签。标签有一个父属性,指向它标记的控件。该函数非常通用,而子例程是为 OptionButtons 编写的。这段代码几乎适用于任何事情,除非标签没有关联,我没有为此提供任何恢复。
Public Function paNameControlLabel(FormName As String, ControlName As String) As String Dim frm As Form Dim ctl As Control Dim ctlLabel As Control Dim ctlParent As Control
Public Function paNameControlLabel(FormName As String, ControlName As String) As String Dim frm As Form Dim ctl As Control Dim ctlLabel As Control Dim ctlParent As Control
Set frm = Forms(FormName)
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acLabel
If ctl.Parent.Name = ControlName Then
Debug.Print "Label " & ctl.Name & " Renamed to lbl" & ControlName
ctl.Name = "lbl" & ControlName
paNameControlLabel = ctl.Name
End If
End Select
Next ctl
End Function Public Sub paNameOptionButtonLabels(FormName As String) Dim frm As Form Dim ctl As Control
End Function Public Sub paNameOptionButtonLabels(FormName As String) Dim frm As Form Dim ctl As Control
Set frm = Forms(FormName)
For Each ctl In frm.Controls
If ctl.ControlType = acOptionButton Then
Debug.Print paNameControlLabel(FormName, ctl.Name)
End If
Next ctl
Set frm = Nothing
End Sub
结束子
回答by Francis Omoruto
Probably late, but I just struggled with this, and what worked was to check the type of control to make sure it supports labels, and then use
可能晚了,但我只是为此而苦苦挣扎,有效的是检查控件的类型以确保它支持标签,然后使用
ctl.Properties(3) ' For the label name
Forms(ctl.Form).Controls(ctl.Properties(3)).Caption ' For the label text
In the Immediate window, using a text index instead of the magic number: ctl.properties("LabelName")worked.
在立即窗口中,使用文本索引而不是幻数:ctl.properties("LabelName")有效。
At first I looked for the label among the properties of the textbox and struck out. I then looked among the controls of the form and found it there.
起初我在文本框的属性中寻找标签并删除。然后我查看了表单的控件并在那里找到了它。
Hope this helps someone out!
希望这可以帮助别人!
回答by JonH
If its access I think it is
如果它的访问我认为是
Forms!YourFormName!YourField.Value
Forms!YourFormName!YourField.Value
Or if you have a sub form its:
或者,如果您有一个子表单:
Forms!yourMainForm!YourSubForm!YourField.Value
Forms!yourMainForm!YourSubForm!YourField.Value

