By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,490 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,490 IT Pros & Developers. It's quick & easy.

How to test if a label is linked to a textbox

Seth Schrock
Expert 2.5K+
P: 2,931
I'm currently using the following code to reference the label that is linked to a textbox.
Expand|Select|Wrap|Line Numbers
  1. Forms(frm).Controls(ctl.Name).Controls.Item(0).ForeColor = vbBlack
frm is a parameter passed to the sub and contains the name of the calling form. ctl is declared as a control and is populated by looping through the controls in the form. Anyway, back to my question. If there isn't a label linked to the textbox, I get an error saying object doesn't exist (as would be expected, since it really doesn't exist). Is there a way to test if the object does exist so that I can do something else with the textbox instead of its label?
Feb 6 '14 #1

✓ answered by NeoPa

I think it works the other way too Seth. If a label has a parent of another control then it is linked to the other control. If a TextBox has a linked label then it'll be in its .Controls collection.

So, if Forms(frm).Controls(ctl.Name).Controls.Count > 0 then I would expect it to have a linked label. If not, not.

Share this Question
Share on Google+
7 Replies


ADezii
Expert 5K+
P: 8,597
The Parent property of a label control returns an Object Reference to the Control the Label is linked to. If the Parent Property of a Label returns the Form Name, then it is not associated with a Text Box as illustrated below where 3 Labels have no association with any Text Box.
Expand|Select|Wrap|Line Numbers
  1. Dim obj As Object
  2. Dim ctl As Control
  3.  
  4. Debug.Print "Label", "Parent"
  5. Debug.Print "--------------------------------------"
  6.  
  7. For Each ctl In Me.Controls
  8.   If ctl.ControlType = acLabel Then
  9.     Set obj = ctl.Parent
  10.       Debug.Print ctl.Name, obj.Name, IIf(obj.Name = "Form2", "NOT LINKED", "")
  11.   End If
  12. Next
  13.  
  14. Debug.Print "--------------------------------------"
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Label         Parent
  2. --------------------------------------
  3. Label5        State         
  4. Label7        AccountNumber 
  5. Label9        cboProducts   
  6. Label22       txtTest       
  7. Label23       EmployeeID    
  8. Label24       LastName      
  9. Label27       Form2         NOT LINKED
  10. Label28       Form2         NOT LINKED
  11. Label10       Form2         NOT LINKED
  12. --------------------------------------
Feb 6 '14 #2

Seth Schrock
Expert 2.5K+
P: 2,931
My problem is that I'm working from the textbox, not the label. So basically, I need something like
Expand|Select|Wrap|Line Numbers
  1. If Exists(Forms(frm).Controls(ctl.Name).Controls.Item(0)) Then
  2.     'Work with the label
  3. Else
  4.     'Work with the textbox
  5. End If
I just need to know how to create the Exists() function. I don't need it to be a separate function, but I need the functionality of it. I just don't know how to do that.

It is useful to know about the parent property of the label though.
Feb 6 '14 #3

ADezii
Expert 5K+
P: 8,597
@Seth:
If all else fails, you can always use reverse Logic. I will not bore you with an explanation, but simply Post how this can be done.
  1. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fFindLabel(txt As TextBox, frm As Form)
    2. Dim ctl As Control
    3. Dim blnLabelFound As Boolean
    4.  
    5. For Each ctl In frm.Controls
    6.   If ctl.ControlType = acLabel Then
    7.     Set obj = ctl.Parent
    8.       If obj.Name = txt.Name Then       
    9.         blnLabelFound = True
    10.           Exit For
    11.       End If
    12.   End If
    13. Next
    14.  
    15. If blnLabelFound Then
    16.   'Set the ForeColor of the associated Label to Red
    17.   ctl.ForeColor = vbRed
    18. Else
    19.   'No associated Label, set Forecolor of Text Box to Blue
    20.   txt.ForeColor = vbBlue
    21. End If
    22. End Function
  2. Sample Calls (with/without associated Label):
    Expand|Select|Wrap|Line Numbers
    1. 'Has an associated Label
    2. Call fFindLabel(Me![EmployeeID], Me)
    3.  
    4. 'No Label associated with this Text Box
    5. Call fFindLabel(Me![Text30], Me)
Feb 6 '14 #4

NeoPa
Expert Mod 15k+
P: 31,186
I think it works the other way too Seth. If a label has a parent of another control then it is linked to the other control. If a TextBox has a linked label then it'll be in its .Controls collection.

So, if Forms(frm).Controls(ctl.Name).Controls.Count > 0 then I would expect it to have a linked label. If not, not.
Feb 7 '14 #5

Seth Schrock
Expert 2.5K+
P: 2,931
Ah, that is what I was looking for. Works perfectly. Thanks NeoPa.

And thank-you Adezii for your help. I will definitely put your solution in my bag of tricks.
Feb 7 '14 #6

ADezii
Expert 5K+
P: 8,597
@NeoPa:
Very nice response, NeoPa. I never even considered this possibility since the Controls Collection is applicable to only Forms, Sub-Forms, Reports, and Sections. Very intuitive on your part. That's what I love about this place, you can learn something new every day! (LOL).

BTW, replacing my dozen and a half lines of Code with one is a great trade-off.
Feb 7 '14 #7

NeoPa
Expert Mod 15k+
P: 31,186
ADezii:
...the Controls Collection is applicable to only Forms, Sub-Forms, Reports, and Sections.
I'm not sure that statement is quite right my friend. I didn't before, but looking at the Help page for Controls in Access 2003 (The last version that seems to have a useful Help system) it says :
The Controls collection contains all of the controls on a form, report, or subform, within another control, or attached to another control. The Controls collection is a member of a Form, Report, and SubForm objects.
Easy to misinterpret as it seems to contradict itself to be fair. And further down :
Other Control objects have a Controls collection that can contain an attached label. These controls include the text box, option group, option button, toggle button, check box, combo box, list box, command button, bound object frame, and unbound object frame controls.
In this case I didn't look into Help as Seth had already made a reference to it in his original code. All I did was know that if there was a Controls collection there then the .Count property should indicate whether or not it had a label. I couldn't think of another control that a TextBox might be associated with in any other way.

Anyway, all good. An interesting snippet we all learned from I expect :-)
Feb 7 '14 #8

Post your reply

Sign in to post your reply or Sign up for a free account.