With vba how can I figure out the control a label is associated with?

I would like to programmatically find a labels associated input field.

I guess it is in one of the control's (label's) properties.
But which one?

I ran this to a file but I couldn't guess which one it was.

    Dim s As String
    Dim cont As Long
   
    cont = 1
    On Error Resume Next
   
    For counter = 0 To Forms(0).Controls(cont).Properties.Count - 1
        s = s & counter & " - " & Forms(0).Controls(cont).Properties(counter).Name & "  =  " & Forms(0).Controls(cont).Properties(counter).Value & vbCrLf
    Next


stevenuuAsked:
Who is Participating?
 
mbizupConnect With a Mentor Commented:
In explanation -

The associated control is the label's "Parent".  The Name property is used to return the Parent Control's name.

Parent.name can be used in other contexts too:

Me.YourLabel.Parent.Name --> name of the associated control
Me.YourTextbox.Parent.Name --> name of the form the textbox resides on
Me.Parent.Name (from a subform)  --> the name of the "subform control" on the mainform

... etc
0
 
mbizupCommented:
Me.YourLabel.Parent.Name
0
 
Dale FyeCommented:

Keep in mind, that not all labels have parent controls, some have the form as the parent contol.

So if you want to check whether a label has an associated input control (textbox, list, combo, checkbox, ...) you might do

Dim ctrl as control
Dim strField as string

if me.YourLabel.Parent.Name <> me.Name Then

    set ctrl = me.yourlabel.parent

    'If you are actually looking for the "field" that the control is bound to, you would need to then
    'look at the ControlSource of the control
    strField = ctrl.ControlSource

End If    
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.