For Each ??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form with multiple check boxes, on the after update event I change
the label for each chk box to a different font color. Works great, however
when the form looses the focus it doesn't maintain the font color if the box
is check. So here is my question.....should i have something on the On
Current event for that form that check each box and if it is checked, set the
font color? If so, can someone supply a sample of exactly how to do that???
 
You didn't post your control after update event code, so you will have to
modify the following code.

I made a form and set the default view to "Single Form". I added 5 check
boxes named "chk1" thru "chk5". So the labels were named "chk1_Label" thru
"chk5_Label".

Paste the following code in the form's current event:

'----------------------------------------------------------------
Private Sub Form_Current()
Dim ctl As Control
Dim x As Integer

x = 0
'color labels
For Each ctl In Me.Controls
If ctl.ControlType = acCheckBox Then
If ctl Then
x = x + 1
Select Case x
Case 1
Me.Controls(ctl.Name & "_Label").ForeColor = vbRed
Case 2
Me.Controls(ctl.Name & "_Label").ForeColor = vbBlue
Case 3
Me.Controls(ctl.Name & "_Label").ForeColor = vbGreen
Case 4
Me.Controls(ctl.Name & "_Label").ForeColor = vbYellow
Case 5
Me.Controls(ctl.Name & "_Label").ForeColor = vbMagenta
End Select
Else
'check is False, set the label font color to black
Me.Controls(ctl.Name & "_Label").ForeColor = vbBlack
End If
End If
Next ctl
End Sub
'----------------------------------------------------------------

This is just one way. You could also hard code the names of the
checkbox/labels and do away with the "For Each..." syntax.


HTH
 
Thanks Steve. I noticed in your code you use actual color names, I used the
numerical values, would that make a difference?
 
Nope. I just used VBA constants for the colors... it is easier for me to
remember vbRed instead of the RGB numbers. But using the RGB numbers you can
control the shades of the colors (like pink or orange).
 
Ok, Steve what have I done wrong? I used the exact you gave and it does work
with the exception that once i move to a new form it maintains the color (not
black) label once any record as had the check box updated. I did put the
code on the OnCurrent event so I am lost as to why it would do this. Any
thoughts?

Thanks
 
Open the form in design view. Bring up the properties dialog. Look at the
events tab. View the code for the form current event. (The code might have
come unhooked).

You might also put a breakpoint in the code at the start of the Form Current
code to see if the sub is being run (ran??).
 
Back
Top