Button Color based on value in lookup form.

spokaneneon

New member
Local time
Today, 11:34
Joined
Oct 7, 2011
Messages
8
Hey guys! I have been using this site for awhile now and what a huge reference you have all been! Here is my coding dilemma.

I have a form that basically serves as a record browser. This form has control sources linked to the main table. IE: Tech Name, User ID, Pass, Fail ect...

What I want to do is when browsing the form, it loads the values fine into my buttons, and shows them as enabled, however, all I want to do is have the buttons be color coded based on Pass or Fail. Basically, if Pass is TRUE in my table, I want the button to be colored green, if Fail is TRUE I want the Fail button to be colored Red. The default value of grey or whatever is fine if it is not true/selected.

I have tried a few different lines of code, however when switching through records, it does not change colors accordingly. Here is what I have tried in my Form Load:

Private Sub Form_Load()
If Me.Toggle270 = True Then
Me.Toggle270.ForeColor = vbGreen
Else
Me.Toggle270.ForeColor = vbBlack
End If
End Sub

and have tried:

Private Sub Toggle270_AfterUpdate()
If Me.Toggle270 = True Then
Me.Toggle270.ForeColor = vbGreen
Else
Me.Toggle270.ForeColor = vbBlack
End If
End Sub
Private Sub Toggle270_BeforeUpdate(Cancel As Integer)
If Me.Toggle270 = True Then
Me.Toggle270.ForeColor = vbGreen
Else
Me.Toggle270.ForeColor = vbBlack
End If
End Sub

even in my before and after updates. For now, Pass is labed as Toggle270 and fail is labeled Toggle271.

Any ideas? Thanks for you're help!
 
Welcome to the forum!

You can look into Conditional Formatting.
 
Thanks, didn't even think of that! I know how that works in Excel, will do some digging!
 
Oki doki! Let us know if you are having problems.
 
Hmm... can't seem to find anything that works. Conditional Formatting only seems to want to work with labels and text boxes. :(
 
So there's no Conditional Formatting for Toggle buttons? In that case, move your original code to the form's On Current event.

If it's a Continuous form or in Datasheet view it won't work.
 
Awesome, that did the trick perfectly! Now if only I could get the whole button to change color instead of the text, we would be in business... from all I have been able to find, I have to do it with a bitmap. *sigh*
 
Does the Toggle button have a Back Color property? If it doesn't then you can't change the back colour. And even if you can, buttons normally have to have their Back Style property set to Transparent for the back colour to take effect.
 

Users who are viewing this thread

Back
Top Bottom