It can become necessary to need to determine the associated label to a given control. It actually is, yet again, very simple to do. Not what I’d call intuitive, but easy once you are aware of the proper syntax to use.
To reference a control’s associated label you use the following syntax
Me.ControlName.Controls(0)
Or
Forms!FormName.Form.ControlName.Controls(0)
Another alternative approach would be
Me.ControlName.Properties("LabelName")
So let’s say we wanted to determine a control’s associated label’s caption, we do something along the lines of:
Me.ControlName.Controls(0).Caption
Error number: 2467 – The expression you entered refers to an object that is closed or doesn’t exist.
that may arise with control’s that do not have associated labels.
Thank you very much you have saved me!
Old thread but great info!
Good! I knew this but what about getting the textbox that is associated with a label?. I have the label and want to get its textbox… It must be posible since when i clic on a label its textbox get the focus but i do not find any information about this.
Thank you.
I’m not sure it is possible. Even when you export the form to a Text file, I don’t see any property with this information.
You’re best bet is probably to create a simple form control loop checking each control’s associated label, until you find the label you are searching for.
If a label is associated with a control you can use the label control’s Parent property to get a reference to the associated control, e.g. MyLabel.Parent.Name. If the label is not associated with a control then the Parent property will refer to the parent form/report.
Thank you for sharing Bruce. This is great to know. I taken the liberty to create a small post on the matter with a reusable function.
https://www.devhut.net/access-determine-a-labels-associated-control/
Bonjour,
Pour gérer l’erreur j’utilise la propriété Count :
Debug.Print CtrLabelName(Me.MyControl)
Private Function CtrLabelName(oCtr As Control) As String
If (oCtr.Controls.Count > 0) Then CtrLabelName = oCtr.Controls(0).Name
End Function