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

Access an attached label in VBA

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
Thanks a lot. I will give it a try.

Troy Lee
Sep 24 '08 #3

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.