• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

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


0
stevenuu
Asked:
stevenuu
  • 2
1 Solution
 
mbizupCommented:
Me.YourLabel.Parent.Name
0
 
mbizupCommented:
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
 
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

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now