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

Posted on 2011-10-08
Last Modified: 2013-11-28
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

Question by:stevenuu
    LVL 61

    Expert Comment

    LVL 61

    Accepted Solution

    In explanation -

    The associated control is the label's "Parent".  The Name property is used to return the Parent Control's 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
    LVL 47

    Expert Comment

    by:Dale Fye (Access MVP)

    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    

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
    Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now