Change a graphic depending on the value in a field

G

Guest

I'm trying to set a button color (red, yellow, green) depending on the value
entered in a field. Is there a way to do this in Access?
 
B

Brendan Reynolds

It's easier if you use a label rather than a command button, then you can
just change the BackColor property. But here's an example that demonstrates
changing both the Picture property of a command button and the BackColor
property of a label ...

Private Sub Text2_BeforeUpdate(Cancel As Integer)

Select Case Me.Text2
Case "1"
Me.Command0.Picture = "c:\usenet\red.bmp"
Me.Label1.BackColor = vbRed
Case "2"
Me.Command0.Picture = "c:\usenet\yellow.bmp"
Me.Label1.BackColor = vbYellow
Case Else
Me.Command0.Picture = "c:\usenet\blue.bmp"
Me.Label1.BackColor = vbBlue
End Select

End Sub
 
G

Guest

Command buttons do not have a back color property, so you can't actually
change the color of the button. You can change the color of the text. That
you can do in the After Update event of the text box the color depends on.
You will also need to do the same thing in the form's Current event so the
color will change to the correct color for existing records:

Select Case Me.txtSomeControl
Case is "Frodo"
Me.cmdBuz = vbRed
Case is "Bullpucky"
Me.cmdBuz = vbYellow
Case Else
Me.cmdBuz = vbGreen
End Select

The other way to do this is to create bmp image files with the colors and
text you need for the button and use the same logic as above to change the
control's picture property.
 
G

Guest

Ed,

You can change the color of the text easily by using Me.myCmdBtn.ForeColor =
[color code], but changing the background color requires a little more
creativity.

You have to make your own button from a label. Set its special effect
property to "raised", its background color to the color needed. If you want
it to "press" when clicked, use the mousedown event + repaint to change
special effect to sunken then change back to raised with mouseup + repaint.
Finally, you'll need to set the top margin for the text to get it centered
vertically on the button.

To make it "disabled" is another whole group of formatting codes. Just
expirament.

HTH,
Bruce
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top