473,505 Members | 14,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access an attached label in VBA

I have a sub with this code:

Sub ColorLabel()
Dim ctlCurrentControl As Control
Dim lngYellow As Long

lngYellow = RGB(255, 255, 0)

Set ctlCurrentControl = Screen.ActiveControl
With ctlCurrentControl.???
.BackColor = lngYellow
End With
End Sub

I want to run this code On Enter of a text box. Basically, I want to
color the label of the active text box. I am calling the procedure on
enter and will call another sub to restore the default formatting of
the label once the text box loses focus.

I have found how to reference a text box using a label's parent
property but I want to reference the label attached to the active text
box but don't know which property to reference (thus the question
marks above). Can anyone tell me how this is done or if it's even
possible?

Thanks in advance.

Troy Lee
Sep 24 '08 #1
7 7333
tr******@comcast.net wrote:
I have a sub with this code:

Sub ColorLabel()
Dim ctlCurrentControl As Control
Dim lngYellow As Long

lngYellow = RGB(255, 255, 0)

Set ctlCurrentControl = Screen.ActiveControl
With ctlCurrentControl.???
.BackColor = lngYellow
End With
End Sub

I want to run this code On Enter of a text box. Basically, I want to
color the label of the active text box. I am calling the procedure on
enter and will call another sub to restore the default formatting of
the label once the text box loses focus.

I have found how to reference a text box using a label's parent
property but I want to reference the label attached to the active text
box but don't know which property to reference (thus the question
marks above). Can anyone tell me how this is done or if it's even
possible?
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Labels are separate controls in Access VBA, i.e., they are not
properties of the attached TextBox. What I do is name the label similar
to the TextBox I will be working on, e.g.:

TextBox name: txtFirstName
Lable name: lblFirstName

Then when the TextBox has the focus I just do something like this to get
to the Label's properties:

Dim ctlLabel As Label

Set ctlLabel=Controls(Replace(Screen.ActiveControl.Nam e, "txt","lbl"))

ctlLabel.BackColor = vbYellow

--
MGFoster:::mgf00 <atearthlink <decimal-pointnet
Oakland, CA (USA)
** Respond only to this newsgroup. I DO NOT respond to emails **

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBSNms54echKqOuFEgEQIq1gCfWBwOHIDhoHti4oKa8Ln7Ny jU62MAnRwL
WNwMMau5JpIecj/mdrGMCzj9
=TxSH
-----END PGP SIGNATURE-----
Sep 24 '08 #2
Thanks a lot. I will give it a try.

Troy Lee
Sep 24 '08 #3
rkc
On Sep 24, 8:46*am, troy_...@comcast.net wrote:
Thanks a lot. I will give it a try.

Troy Lee
A label attached to a textbox would be the one and only control in the
textboxes controls collection.

With ctlCurrentControl
.Controls(0).BackColor = lngYellow
End With

Sep 24 '08 #4
On Sep 24, 7:22 pm, rkc <r...@rkcny.comwrote:
On Sep 24, 8:46 am, troy_...@comcast.net wrote:
Thanks a lot. I will give it a try.
Troy Lee

A label attached to a textbox would be the one and only control in the
textboxes controls collection.

With ctlCurrentControl
.Controls(0).BackColor = lngYellow
End With
Wow, that's amazing. The more I work with VBA the more I like it.

Thanks a lot.

Troy Lee
Sep 25 '08 #5
rkc <rk*@rkcny.comwrote:
>A label attached to a textbox would be the one and only control in the
textboxes controls collection.

With ctlCurrentControl
.Controls(0).BackColor = lngYellow
End With
Interesting. I was quite unaware of that syntax. Although I'd just use

ctlCurrentControl.Controls(0).BackColor = lngYellow

I don't mind using With and End With for a large number of lines but for only one not
worth it.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
Sep 25 '08 #6
On Sep 24, 7:22*pm, rkc <r...@rkcny.comwrote:
A label attached to a textbox would be the one and only control in the
textboxes controls collection.

With ctlCurrentControl
* * * * * *.Controls(0).BackColor = lngYellow
End With
Good One! Of course, I had to test the one and only one statement. I
couldn't add two child labels to a textbox with the gui nor in code. I
didn't try other kinds of children, but I'm guessing they wouldn't add
either. So it seems one and only is true.

As an aside to the OP, vbYellow exists as an intrinsic constant making
lngYellow unnecessary.

Dim lngYellow@
lngYellow = RGB(255, 255, 0)
Debug.Print lngYellow = vbYellow
' True

It may not be commonly known that we can more or less code color
intuitively by using simple math with the vb color constants. For
instance (vbRed + vbYellow / 2) gives a deep orange.
Sep 25 '08 #7
rkc <rk*@rkcny.comwrote:
>A label attached to a textbox would be the one and only control in the
textboxes controls collection.

With ctlCurrentControl
.Controls(0).BackColor = lngYellow
End With
BTW I decided to blog this tip. See
http://msmvps.com/blogs/access/archi...l-s-label.aspx

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
Oct 4 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
2267
by: Remco Groot Beumer | last post by:
Hello, I'm trying to decide if the following situation would be workable: Generate an MS Access Front End (which will run localy on client computers), which will link to a DBMS (SQL server or...
1
1367
by: DD | last post by:
As the user will not have access to the database, i want them to be able to click a button and open the create a new label,based on a default report i will have saved in the database. Can anyone...
2
1851
by: Galina | last post by:
Hello I have a very complex database application, which have been working fine for several years in Access 97. Now I had to convert it into Access 2000. The main form (course) has got 2 subforms....
6
2089
by: MLH | last post by:
You know how the text in the attached label sort of 'greys out' when you click something that sets MyTextbox.Enabled = False? I would like to make a label's text look like that? How do I do it?...
1
1741
by: bill yeager | last post by:
I did some more debugging and found the following: 1) I placed the following code in the button event just to see if I could cycle thru the datagrid control collection: <code> Dim strhello As...
3
1741
by: Rahul Agarwal | last post by:
Hi In our web page we use a combination of HTML and server side controls and some of them have a custom attribute based on which we need to find and replace the values once the HTML is ready. ...
4
1289
by: John Mark Howell | last post by:
I have a form with five programatically created labels and a datagrid. I want to drag a row from the grid to the label. I have attached a single DragOver event handler (via AddHandler) to each...
0
1173
by: Miloann | last post by:
We are implementing an application that sends reminder emails to employees in other departments through intranet. The application comes with an email template in ASPX. Can I modify the codes to...
16
6486
by: JoeW | last post by:
I'm utilizing a database that I created within MS Access within a program I've created in VB.NET. I am using the VB front end to navigate the information, but want to be able to print a report,...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
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
7471
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...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.