Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2002
    Location
    Mpls/St.Paul area
    Posts
    303

    Unanswered: programmatically find a control's label

    Is there a way to refer to a control's label if you only know what the control name is?

    For example, using VBA, and cycling through all controls on a form, can you get the caption from the associated label - when you don't know what the name of the label is?
    John
    This is the day the Lord has made, I will rejoice and be glad in it.

  2. #2
    Join Date
    Nov 2002
    Location
    Belgium
    Posts
    243

    Re: programmatically find a control's label

    Originally posted by jpshay
    Is there a way to refer to a control's label if you only know what the control name is?

    For example, using VBA, and cycling through all controls on a form, can you get the caption from the associated label - when you don't know what the name of the label is?
    I don't know if it's possible, but I think you might solve the problem just by assigning structured names to the controls.
    If you have a control (let's say a textfield with its label)
    If you name the textfield 'txtMyTextField' and the label 'lblMyLabel' than it can't be so difficult to understand which label belongs to which textfield.

  3. #3
    Join Date
    Jun 2002
    Location
    Mpls/St.Paul area
    Posts
    303

    Question

    Thanks for the feedback.

    I do label option groups, text boxes combo boxes with labels because of the need to reefer to them with code. But labeling their labels would be twice the amount of work. With small database, no big deal; with multiple databases, multiple forms, with 50 to 100 controls per for, it becomes onerous.

    So, is there a way with VBA, to refer to the label of the current control?

    John
    This is the day the Lord has made, I will rejoice and be glad in it.

  4. #4
    Join Date
    Aug 2003
    Location
    Delft, The Netherlands (EU)
    Posts
    447
    I guess, you can use the form's control collection:

    Control("<Control Name>").Label
    Make everything as simple as possible, but not simpler! - A. Einstein
    DB Problems? DB Explorer, BTrieve Re-engineering, DB Conversions & ETL? Conversion Tool

  5. #5
    Join Date
    Jun 2002
    Location
    Mpls/St.Paul area
    Posts
    303
    Thanks for the response, DoktorBlue.

    I tried the suggetion, but don't see how to make the syntax work.

    Lets say I have a form with a text box control named "txtName." Associated with this text box control is the label, "lbl1"

    I can refer the label as me.lbl1. From here I can change format properties, the caption, etc.

    I can refer to the text box in VBA as me.txtName and then get to a whole host of properties. Its fontsize, controlsource, backcolor, etc.
    Regarding label properties for the control, all I see is three properties: LabelAlign, LabelX, LabelY.

    Now, lets say I don't know the name of the associated label. Is there a way to change or get the label caption from lbl1 if I am currently accessing me.txtName?
    John
    This is the day the Lord has made, I will rejoice and be glad in it.

  6. #6
    Join Date
    Aug 2003
    Location
    Delft, The Netherlands (EU)
    Posts
    447
    Ohh, it΄s

    me.controls("txtname").label


    I forgot the 's'


    You can also loop through the control collection:


    dim c as control
    for each c in me.controls
    msgbox c.label
    next c
    Last edited by DoktorBlue; 03-20-04 at 07:30.
    Make everything as simple as possible, but not simpler! - A. Einstein
    DB Problems? DB Explorer, BTrieve Re-engineering, DB Conversions & ETL? Conversion Tool

  7. #7
    Join Date
    Mar 2012
    Posts
    3

    try this

    Hi, in my case I normally navigate thru my controls in the following way:

    Dim ctl As Control


    For Each ctl in Form
    If ctl.ControlType = acTextBox or ctl.ControlType = acComboBox Then
    'some action
    'some action
    End If
    Next

    if I want display the label caption I use the following syntax:

    MsgBox "Some message " & ctl.Controls(0).Caption

    if you want the label name use: ctl.Controls(0).Name

    Hope this help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •