469,275 Members | 1,514 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

How to change label color of checkbox when checked?

37
I am trying to change the label color of a checkbox on a subform if it is checked.

The code I have so far is:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Emergency_Contact_Click()
  2. If Me.Emergency Contact = True Then
  3. Me.Label23.BackColor = vbYellow
  4. Me.Label23.BackStyle = 1
  5. Else
  6. Me.Label23.ForeColor = RGB(0, 0, 0)
  7. Me.Label23.BackStyle = 1
  8. End If
  9. End Sub
The problem is that when the checkbox is checked, the label is highlighted for all records. I only want the label to be highlighted for the individual record in which the checkbox is checked.

Is there a work-around for this problem?

Thanks,
Yappy
Mar 2 '11 #1

✓ answered by NeoPa

You seem to be misunderstanding the different types of values :
"-1" <> -1
"Yes" <> Yes

The two values on the left are string values, whereas those on the right are numeric (Yes is a Boolean, but also numeric). If you must turn the TextBox value into a string (It would probably suit your purpose better to keep it as the value of [Emergency Contact] and simply use the .Format property to display Yes or No as required.), I would use the following for your IIf() statement as comparing a Boolean value to True or False is superfluous (as it already is either True or False) :
Expand|Select|Wrap|Line Numbers
  1. =IIf([Emergency Contact], 'Yes', 'No')

5 12791
NeoPa
32,172 Expert Mod 16PB
You could look at Conditional Formatting, but otherwise it can't work. See Why Values in Unbound Form Controls do not Persist if you need an explanation why.

Conditional Formatting only works with controls that contain data of course though, so you will need to be a little creative.
Mar 2 '11 #2
yappy77
37
Conditional Formatting is greyed out when I click on the label or the check box in design view. Does this mean I am unable to use conditional formatting?
Mar 2 '11 #3
NeoPa
32,172 Expert Mod 16PB
Certainly not for those controls (The label contains no data and the CheckBox has nothing to format).

If you were to create a TextBox though, that reflected the value in the CheckBox, this could be used to highlight checked records. This was where I was leading when I posted "so you will need to be a little creative".
Mar 2 '11 #4
yappy77
37
Ok. So I created the text box as you suggested and set the conditional formatting. The formatting is not working. Here is the code that I have in the text box:

Expand|Select|Wrap|Line Numbers
  1. =IIf([Emergency Contact]="-1","Yes","NO")
The conditional formatting is as follows:
Field Value is equal to Yes then fill is Yellow

Please help. Thanks!
Mar 7 '11 #5
NeoPa
32,172 Expert Mod 16PB
You seem to be misunderstanding the different types of values :
"-1" <> -1
"Yes" <> Yes

The two values on the left are string values, whereas those on the right are numeric (Yes is a Boolean, but also numeric). If you must turn the TextBox value into a string (It would probably suit your purpose better to keep it as the value of [Emergency Contact] and simply use the .Format property to display Yes or No as required.), I would use the following for your IIf() statement as comparing a Boolean value to True or False is superfluous (as it already is either True or False) :
Expand|Select|Wrap|Line Numbers
  1. =IIf([Emergency Contact], 'Yes', 'No')
Mar 7 '11 #6

Post your reply

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

Similar topics

7 posts views Thread by Don | last post: by
2 posts views Thread by bebop | last post: by
7 posts views Thread by Lars Netzel | last post: by
16 posts views Thread by GTi | last post: by
1 post views Thread by Chris | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.