MS Access – VBA – Determine a Control’s Associated Label

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

 
 

Caution - Error Handling Required!
One note of caution however, you need to ensure you implement error handling to trap possible errors

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.

7 responses on “MS Access – VBA – Determine a Control’s Associated Label

  1. David

    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.

    1. Daniel Pineault Post author

      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.

      1. Bruce Hulsey

        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.

  2. Laurent

    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