Changing Labels Color Via Code

G

Guest

I have a questionnaire with many check boxes for collection y/n data. I
have a requirement to change the color of the label associated with the
check box if it is selected or unselected (exact colors don't). I want to
use a function that can be called whenever one of the controls of concern
are changed.

We use Windows 2k and Access97 with all service packs.
Sample of the on change event code for one of the check boxes:

Private Sub chkOne_AfterUpdate()
Dim strControlName As String
Dim bolSelected As Boolean
strControlName = "chkOne"
bolSelected = Me.chkOne
Call ChangeColor(strControlName, bolSelected)
End Sub

One of the experimental functions that is attempting to fulfill the
requirement which does not come close to working:

Function ChangeColor(strControlName As String, bolSelected As Boolean)
If bolSelected Then
Me." & strControlName & ".BackColor = vbYellow
Else
Me." & strControlName & ".BackColor = vbBlue
End If
End Function

Thanks for looking,
Bob
 
M

Marshall Barton

I have a questionnaire with many check boxes for collection y/n data. I
have a requirement to change the color of the label associated with the
check box if it is selected or unselected (exact colors don't). I want to
use a function that can be called whenever one of the controls of concern
are changed.

We use Windows 2k and Access97 with all service packs.
Sample of the on change event code for one of the check boxes:

Private Sub chkOne_AfterUpdate()
Dim strControlName As String
Dim bolSelected As Boolean
strControlName = "chkOne"
bolSelected = Me.chkOne
Call ChangeColor(strControlName, bolSelected)
End Sub

One of the experimental functions that is attempting to fulfill the
requirement which does not come close to working:

Function ChangeColor(strControlName As String, bolSelected As Boolean)
If bolSelected Then
Me." & strControlName & ".BackColor = vbYellow
Else
Me." & strControlName & ".BackColor = vbBlue
End If
End Function


The syntax you're looking for is:
Me(strControlName).BackColor = vbYellow

But, there is a better way to do this kind of thing.

Change the function to:

Function ChangeColor()
If Me.ActiveControl = True Then
Me.ActiveControl.BackColor = vbYellow
Else
Me.ActiveControl.BackColor = vbBlue
End If
End Function

Then you can scrap all the AfterUpdate procedures by setting
the AfterUpdate **property** to =ChangeColor() instead of
[Event Procedure]

You don't even have to type that in each check box's
property one at a time. If you select all the check boxes
by holding the Shift key down while clicking on each check
box, you only need to type it in once.
 
G

Guest

Marshall / Bob,
Sorry for interrupting your discussion but I am interested in something
similar.
I would like to add to Bob’s question by asking Marshall if it would be
possible to change the background colour of another field in that record
after checking the checkbox. I have a need to grey the background of the
description field when the checkbox is checked and clear if un-checked.

Your assistance is appreciated
Regards,
Nick


Marshall Barton said:
I have a questionnaire with many check boxes for collection y/n data. I
have a requirement to change the color of the label associated with the
check box if it is selected or unselected (exact colors don't). I want to
use a function that can be called whenever one of the controls of concern
are changed.

We use Windows 2k and Access97 with all service packs.
Sample of the on change event code for one of the check boxes:

Private Sub chkOne_AfterUpdate()
Dim strControlName As String
Dim bolSelected As Boolean
strControlName = "chkOne"
bolSelected = Me.chkOne
Call ChangeColor(strControlName, bolSelected)
End Sub

One of the experimental functions that is attempting to fulfill the
requirement which does not come close to working:

Function ChangeColor(strControlName As String, bolSelected As Boolean)
If bolSelected Then
Me." & strControlName & ".BackColor = vbYellow
Else
Me." & strControlName & ".BackColor = vbBlue
End If
End Function


The syntax you're looking for is:
Me(strControlName).BackColor = vbYellow

But, there is a better way to do this kind of thing.

Change the function to:

Function ChangeColor()
If Me.ActiveControl = True Then
Me.ActiveControl.BackColor = vbYellow
Else
Me.ActiveControl.BackColor = vbBlue
End If
End Function

Then you can scrap all the AfterUpdate procedures by setting
the AfterUpdate **property** to =ChangeColor() instead of
[Event Procedure]

You don't even have to type that in each check box's
property one at a time. If you select all the check boxes
by holding the Shift key down while clicking on each check
box, you only need to type it in once.
 
S

strive4peace

Hi Bob,

Tagging on to what Marshall said...

if you will want your checkboxes to be colored when you
change records, you can modify Marshall's code a bit to loop
through your controls to color on the OnCurrent Event for a
specified control instead of the ActiveControl

'------------------------
Function ChangeColor(optional pControlname as string)
dim c as control
if len(nz(pControlname)) > 0 then
set c = me(pControlname)
else
set c = me.ActiveControl
end if

If nz(c, false) = true Then
c.BackColor = vbYellow
Else
c.BackColor = vbBlue
End If
set c = nothing
End Function
'------------------------

Because the Controlname is an optional argument, you could
still use this on the AfterUpdate event as Marshall suggested
=ChangeColor()

Name your controls sequentially like chk1, chk2, etc instead
of chkOne

Then, in the OnCurrent event, you can loop through the controls

'------------------------
dim i as integer
for i = 1 to 10
ChangeColor "chk" & i
next i
'------------------------

If you have no need to call this function from another
place, you may want to declare it to be Private

Have an awesome day

Warm Regards,
Crystal

MVP Microsoft Access
strive4peace2006 at yahoo.com



Marshall said:
I have a questionnaire with many check boxes for collection y/n data. I
have a requirement to change the color of the label associated with the
check box if it is selected or unselected (exact colors don't). I want to
use a function that can be called whenever one of the controls of concern
are changed.

We use Windows 2k and Access97 with all service packs.
Sample of the on change event code for one of the check boxes:

Private Sub chkOne_AfterUpdate()
Dim strControlName As String
Dim bolSelected As Boolean
strControlName = "chkOne"
bolSelected = Me.chkOne
Call ChangeColor(strControlName, bolSelected)
End Sub

One of the experimental functions that is attempting to fulfill the
requirement which does not come close to working:

Function ChangeColor(strControlName As String, bolSelected As Boolean)
If bolSelected Then
Me." & strControlName & ".BackColor = vbYellow
Else
Me." & strControlName & ".BackColor = vbBlue
End If
End Function



The syntax you're looking for is:
Me(strControlName).BackColor = vbYellow

But, there is a better way to do this kind of thing.

Change the function to:

Function ChangeColor()
If Me.ActiveControl = True Then
Me.ActiveControl.BackColor = vbYellow
Else
Me.ActiveControl.BackColor = vbBlue
End If
End Function

Then you can scrap all the AfterUpdate procedures by setting
the AfterUpdate **property** to =ChangeColor() instead of
[Event Procedure]

You don't even have to type that in each check box's
property one at a time. If you select all the check boxes
by holding the Shift key down while clicking on each check
box, you only need to type it in once.
 
M

Marshall Barton

When there's only one or a few check boxes involved, you
would be better off setting the description control's
Conditional Formatting. Use the Expression Is: option to
specify the condition: [checkbo] = True. Then select the
format properties you want to use. When you say you want
the description text box to be gray, you may want to use
either the Disabled property or just set the BackColor to
gray.
--
Marsh
MVP [MS Access]


Nick said:
I would like to add to Bob’s question by asking Marshall if it would be
possible to change the background colour of another field in that record
after checking the checkbox. I have a need to grey the background of the
description field when the checkbox is checked and clear if un-checked.


Marshall Barton said:
The syntax you're looking for is:
Me(strControlName).BackColor = vbYellow

But, there is a better way to do this kind of thing.

Change the function to:

Function ChangeColor()
If Me.ActiveControl = True Then
Me.ActiveControl.BackColor = vbYellow
Else
Me.ActiveControl.BackColor = vbBlue
End If
End Function

Then you can scrap all the AfterUpdate procedures by setting
the AfterUpdate **property** to =ChangeColor() instead of
[Event Procedure]

You don't even have to type that in each check box's
property one at a time. If you select all the check boxes
by holding the Shift key down while clicking on each check
box, you only need to type it in once.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top