Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change label colors when yes/no field = yes 2

Status
Not open for further replies.

ehsguy77

IS-IT--Management
Feb 12, 2004
93
US
I'd like to change the label colors on my form from green to red when on yes/no field on the form is set to yes. There is also a subform that I would like the colors to change on as well. If the field is set back to no, the label colors need to revert back to green. Any ideas?
 
Hi

In OnCurrent event of the form something like

Dim lbl as Label

for each lbl in Me.Controls
If MyCheck Then
lbl.forecolor = vbred
else
lbl.forecolor = vbgreen
end if
next

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
I typed in the following code, and now I get a type mismatch error.

Code:
Private Sub Form_Current()

    Dim lbl As Label
    
    For Each lbl In Me.Controls
        If Me.HazMan = Yes Then
            lbl.ForeColor = vbRed
        Else
            lbl.ForeColor = vbGreen
        
        End If
    Next
            
End Sub

Any suggestions?
 
Hi

It would have helped to know on which line error occured

try

If Me.HazMan Then

as I said in my first post

or

If Me.HazMan = True Then


Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Error is on 'For each lbl in Me.Controls'. Could it be because I have combo boxes as well? Or should that work for all controls on the form?
 
First, lbl must be dimmed as Control as it is used when browsing the Controls collection.
Second, you have to test that lbl.ControlType = acLabel

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
OK, try

Private Sub Form_Current()
Dim Lbl As Control
For Each Lbl In Me.Controls
If Lbl.ControlType = acLabel Then
If Check2 Then
Lbl.ForeColor = vbRed
Else
Lbl.ForeColor = vbGreen
End If
End If
Next Lbl
End Sub

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
KenReay,

That last bit of code is getting me closer. Two issues, however. My Fontcolor changes instead of the forecolor of the labels, and the fontcolor only changes if you update the Hazman field, go to next or previous record and return to the modified record (Refresh doesn't work).

Would this code work better on the OnCurrent or afterupdate event of the HazMan control?

Here's the code as it is in my vb editor:
Code:
Private Sub Form_Current()

    Dim lbl As Control
    For Each lbl In Me.Controls
        If lbl.ControlType = acLabel Then
        If Me.HazMan = True Then
            lbl.ForeColor = vbRed
        Else
            lbl.ForeColor = vbGreen
        
        End If
    End If
    Next lbl
                
End Sub
 
Okay, I changed ForeColor to BackColor, and now the label color changes correctly instead of the FontColor. It still needs to refresh or repaint the labels onclick of the HazMan field. Any ideas?
 
Hi

.Forecolor is the property which controls the colour of the text in a label, try .backcolor to chnage the background colour

You did not say you were changing the value of the check box, suggest to put code in after update of check box AND in oncurrent of form. Correct way to do this of course is to put the code in a subroutine and call it rather than repeating it.

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
I'm interested in your subroutine suggestion, if you have time to explain it to me.
 
In the Genaral section of the form module, put

Public Sub ChangeColour()

Dim lbl As Control
For Each lbl In Me.Controls
If lbl.ControlType = acLabel Then
If Me.HazMan = True Then
lbl.ForeColor = vbRed
Else
lbl.ForeColor = vbGreen

End If
End If
Next lbl

End Sub

in the OnCurrent event of the form and in the after update event of the check box put:

ChangeColour

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Excellent. I appreciate all of your help. I have but one more question. How do I use other colors? For example, the color I want to use is 4227072 in the BackColor property window of a label. Can I set the color with
Code:
lbl.BackColor=4227072
?
 
My apologies - I had one more question. How do I make the label controls in a related subform change colors as well?
 
Same basic method, but to address a control in a subform from the main form you use syntax like Me.MySubformCONTROLName.FORM.lblMyLabel.Forecolor = vbRed

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Thanks again, Ken. You've been a great help. I've got another post in the VBA section if you're interested.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top