How to access the label of a control in code

前端 未结 5 590
天命终不由人
天命终不由人 2020-12-19 06:21

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.

<
相关标签:
5条回答
  • 2020-12-19 06:27

    @Astander has provided the correct answer, but keep in mind that not all controls have the same kind of Controls collections.

    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.

    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.

    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).

    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:

      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! :)

    0 讨论(0)
  • 2020-12-19 06:33

    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.

    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

    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

    0 讨论(0)
  • 2020-12-19 06:34

    If its access I think it is

    Forms!YourFormName!YourField.Value

    Or if you have a sub form its:

    Forms!yourMainForm!YourSubForm!YourField.Value

    0 讨论(0)
  • 2020-12-19 06:44

    With the textbox you can try

    Text0.Controls.Item(0).Caption
    

    where Control 0 is the linked label

    0 讨论(0)
  • 2020-12-19 06:45

    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.

    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!

    0 讨论(0)
提交回复
热议问题