Access – Determine a Label’s Associated Control

I’ve previously posted how we can determine the label which is associated with a control:

There, in the comments, David asked how we could do the inverse, that is determine the control associated to a given label.  At the time I wasn’t aware of any simple solution, but today Bruce Hulsey was kind enough to share with us that there was indeed a simple solution.  Building upon his statement, below is a function we can use to determine to which control a label is associated:
 

The Solution

'---------------------------------------------------------------------------------------
' Procedure : GetLabelAssoCtrl
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Determine a Label's associated control
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Late Binding  -> none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' oLbl      : Label object to retrieve the associated control name of
' oFrm      : Form object containing the label control
'
' Return:
' ~~~~~~~
' The function return the name of the associated control, and "" if it isn't associated
' with any control.
'
' Usage:
' ~~~~~~
' GetLabelAssoCtrl(me.lbl_FirstName, me)
'   Returns -> txt_FirstName
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2023-03-31
' 2         2025-03-01              Perfected the function based on feedback from
'                                   Heinzi to avoid issues with tab controls
'---------------------------------------------------------------------------------------
Function GetLabelAssoCtrl(oLbl As Access.Label, oFrm As Access.Form) As String
    Dim bIsAssociated         As Boolean
    Dim sAssoCtrl             As String

    On Error Resume Next
    bIsAssociated = IsNull(oLbl.Parent)
    On Error GoTo Error_Handler
    If bIsAssociated Then
        sAssoCtrl = oLbl.Parent.Name
    End If
    
    GetLabelAssoCtrl = sAssoCtrl

Error_Handler_Exit:
    On Error Resume Next
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Source: GetLabelAssoCtrl" & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

What you need to be aware here is:

  • If a label is associated/linked to a control, the parent will be that control
  • If a label is NOT associated/linked to a control, the parent will be the form itself

This is why I added a check to ensure the returned control name wasn’t the form and instead return “”.

Thank you Bruce for sharing your knowledge!

 

Useful Resources

6 responses on “Access – Determine a Label’s Associated Control

  1. Bruce Hulsey

    Thank you Daniel, I am honored. This site is a tremendous resource and I have long appreciated it and your hard work in creating and maintaining it!

  2. Heinzi

    Nice method, but note that this technique will fail in two edge cases:

    1. If the associated control has the same name as the form (this is possible), the function returns “” instead of the control’s name.
    2. If the label is not associated to a control, but located in a container (for example, on a tab control page), the function returns the name of the tab control page instead of “”.

    Problem 1 can be fixed by replacing the name check with something like `If TypeOf oLbl.Parent Is Form`. I don’t have a good solution for Problem 2, though. (Obviously, explicitly checking against a hard-coded list of container or non-container control types would be a possible workaround).

    1. Daniel Pineault Post author

      Interesting.

      For me 1. is a non-issue as everything has a unique name when I do development. You should use the same name for multiple objects/controls/… Just don’t do it.

      I’ll have to try and find some time to test 2. to make sure I understand your comment, but that would seem to be logical, no?

      1. Heinrich Moser

        Well, it’s logical that the implementation (accessing .Parent) behaves like this.

        My point was that, in this case, your function returns a different value than the one specified in the function description (“The function return the name of the associated control, and “” if it isn’t associated with any control.”)

        1. Daniel Pineault Post author

          I guess the way to handle it would be to add an extra validation, something like:

          Me.Controls(GetLabelAssoCtrl(oLbl, oFrm)).ControlType

          and check the Control Type with a Case Statement.

          Another possibility that I haven’t had to time to explore and test, but it appears that associated label return Null for oLbl.Parent, but unassociated labels return Run-time error 438 : Object doesn’t support this property or method. So perhaps this is the simplest solution for proper validation.