checkbox label color change

Eljefegeneo

Still trying to learn
Local time
Today, 13:00
Joined
Jan 10, 2011
Messages
902
I have several forms that each has a dozen or more checkboxes, true/false. I want to be able to change the background color of the label of each checkbox depending on if the checkbox is true or false. I have used the following code which works great:
If Me.A = True Then
Me.lblA.BackColor = vbYellow
Me.lblA.BackStyle = 1
Me.lblA.ForeColor = vbRed
Else
Me.lblA.BackColor = vbWhite
Me.lblA.ForeColor = vbBlack
End If
Me.A refers to T/F checkbox A and Me.lblA refers to its label. This is repeated for each of the checkbox controls. If the control is , then its label is lblB. I have used this in the form OnCurrent event and the checkbox control OnUpdate event. I was wondering if I could use the tag property to this, saving me writing countless lines of code and having the OnCurrent event of each of the forms so long and cumbersome. And I do know that I can use conditional formatting to do this, but I would prefer not to use that method.
Is there any way to do this using the tag property and not so many lines of code?
 
There are a couple of things that can simplify the code.

Loop through the Form's Controls Collection and process every checkbox if they are all to be included.

The actual code to modify the properties can be generalised to a separate function that can be called from the loop. This uses the Controls Collection of the checkbox to get its associated label.

Code:
Private Sub Form_Current
   Dim itm as Checkbox
 
   For Each itm in Me.Controls
       If itm.ControlType = 106 Then FormatLabel itm
   Next
 
End Sub
 
Private Sub FormatLabel(chk As Control)
 
   With chk.Controls(0)
 
        If chk Then
            .BackColor = vbYellow
            .BackStyle = 1
            .ForeColor = vbRed
        Else
            .BackColor = vbWhite
            .ForeColor = vbBlack
        End If
 
    End With
End Sub

If only some checkboxes are to be processed then use the Tag propery or a naming pattern to select them in the loop.

Code:
If Left(itm.Name,3) = "xxx" Then
etc
 
If itm.Tag = "xxx" Then
etc
 
Thanks. I will try it tomorrow and let you know.
 
...I do know that I can use conditional formatting to do this...
FYI, that's incorrect! Conditional Formatting, off of the Menu/Ribbon, is not available for Checkboxes or Labels. Your approach using code is the only viable one, I expect, and will only work for Single View Forms.

Linq ;0)>
 
Why bold so much in your posts?

Chris.
 
It can be done with Conditional Formatting which will work in Continuous Forms.

The labels are simulated using textboxes. Lock, disable and remove the border and they act just like a label except they can be formatted.
 
[FONT=&quot]I have tried your suggestion but I am coming up with Run-Time Error 13 Type Mismatch. The Sample DB is attached. What I do not understand is if I use the "[/FONT][FONT=&quot]Private Sub FormatLabel(chk As Control)" instead of making it "Function FormatLabel(chk As Control"), it won't recognize the "FormatLabel" code. If I use Function instead of Private Sub, then it won't recognize the line of code "For Each itm in Me.Controls".[/FONT]


Thanks for your time and patience.
[FONT=&quot][/FONT][FONT=&quot][/FONT]
 

Attachments

I think I have solved it, but am not absolutely sure. If you could take a look at the revised sample DB, I changed on line of code to read: Dim itm As Control, substituting "Control" for "CheckBox".

Also, what is the difference from a Public Sub and a Function?
Thanks.
 

Attachments

Users who are viewing this thread

Back
Top Bottom