473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to test if a label is linked to a textbox

Seth Schrock
2,965 Expert 2GB
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.

7 5391
ADezii
8,834 Expert 8TB
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
2,965 Expert 2GB
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
8,834 Expert 8TB
@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
32,556 Expert Mod 16PB
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
2,965 Expert 2GB
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
8,834 Expert 8TB
@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
32,556 Expert Mod 16PB
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

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

Similar topics

7
by: Rab | last post by:
hi i need to programmatically change the caption of a label that is linked to another control (i.e. textbox, combobox). it's for a generic function where i only know the textbox/combobox name...
0
by: John Dalberg | last post by:
I have a datagrid which has two columns, bound to a database table. One column is static and the other is updatable. Instead of having the edit command button for each row like most grids do, I...
3
by: melanieab | last post by:
Hi, I'd like to put a transparent label over a textbox. I set the label backcolor to transparent, and set its parent as the textbox. But setting the textbox as the parent resulted in the label...
2
by: Dabbler | last post by:
is there an easy way to bind the value of a session variable to a textbox or label instead of setting it in code behind? Thanks much.
3
by: sheenaa | last post by:
Hello friends, I m using ASP.NET with C# 2005 and SQL SERVER 2005 for the ASP.Net Website. I m using sqldatasource to connect and retrieve the data from database and then it displays the data...
4
by: Ken | last post by:
Is there any way to show current webpage in Label or textbox? for example, if the page address I browse currently is http://localhost/try/default.aspx then I want to show the information about...
8
Ali Rizwan
by: Ali Rizwan | last post by:
Hi I want to slide my text as in news but not wanna slide my label or textbox control.
3
by: John Heitmuller. | last post by:
Hi, I just want to make sure I'm not missing something obvious. VB.Net controls like the CheckBox are implemented with a label attached to it. This is very handy. But the TextBox control has no...
3
by: Ronald S. Cook | last post by:
For all those anti-Hungarian notation people out there, how would you name an employee first name label and textbox on an create/modify employee form, respectively (please)? Thanks, Ron
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.