Text Button Colour on Value

A

Alan

Hello All

Looking for a quick bit of advice ... I have a Form which currently contains
numerous buttons. I change the colour of the Font on each button using and If
loop ... ie if 0 then Green if greater than 0 red

If Startup.btn1.Caption = 0 Then
Startup.btn1.ForeColor = 32768
Else: Startup.btn1.ForeColor = vbRed
End If

Given the number of buttons on the form, I am seeking a way that once the
caption value is calculated then one process will do all the red/green
updates ??

Is there a simple way to do this or do I need to leave the cumbersome code ??

Many Thanks for any help
 
K

Klatuu

Call this function from whereever you want to set the colors. You can put it
in the General section if it is specific to a form, or in a standard module
if you want to be able to use it with other forms.

If you want to use it only for one control, you could add another argument
to the function to look for a specific control using the Name property of the
control. If there are buttons you want to exclude from this, you can use the
same technique.

Public Function ColorCtls(strFormName As String) As Boolean
Dim ctls As Controls
Dim ctl As Control
Dim frm As Form

Set frm = Forms(strFormName)
Set ctls = frm.Controls
For Each ctl In ctls
If ctl.ControlType = acCommandButton Then
ctl.Caption = Iif(0, vbGreen, vbRed)
End If
Next
Set ctl = Nothing
Set ctls = Nothing
Set frm = Nothing
 
A

Alan

Many Thanks Klatuu

However this appears to have set all button captions to 255 ???
Am I missing someting ?

Thanks

Alan
 
K

Klatuu

Doh!

Sorry about that

This line
ctl.Caption = Iif(0, vbGreen, vbRed)
should be
ctl.ForeColor = Iif(0, vbGreen, vbRed)
 

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