Color setting in VBA

Dave Eyley

Registered User.
Local time
Today, 05:38
Joined
Sep 5, 2002
Messages
254
Hi All,

I tried to use this on an AfterUpdate on a textbox Control -

If A>B Then
Me!Control.BackColor=255
Else
Me!Control.Backcolor=#FFFFFF
End If

I want it the background to red if A>B and to remain white otherwise but all I get is a black background when it should be white. I tried using the new 2010 convention of Background 1 with and without quotes but nothing seems ot work.
In 2003 you used to get a decimal or hex number option when you used the color picker but that doesn't seem to be the case now.

Any thoughts/help would be appreciated.

Dave
 
Just use CONDITIONAL FORMATTING instead. It works much better. Right click on the control, select CONDITIONAL FORMATTING and then in that area change the FIELD VALUE IS to EXPRESSION IS and put
[A]>

and set the color and font to what you want when that evaluates to true.
 
Thanks for that Bob, but I was hoping to find out how to set background colours for text boxes etc...from VBA...

There must be a way of setting white as a background colour as I managed to set it red using 255
If you set the Back Color on the property sheet for the control to #FFFFFF then it's white, use the statement in VBA - Control.BackColor= #FFFFFF then you get black!

If 255 for red is decimal, then there must be a decimal number for white, right?

Anybody know where I can find this info?

Dave
 
Use the constants
Code:
If A>B Then 
         Me!Control.BackColor = vbRed
Else
         Me!Control.Backcolor = vbWhite
End If
But like Bob said, you are better off with Conditional Formatting.
 
And if you don't want to use constants, you will need to convert the color from HEX to the number Access wants because VBA doesn't use the HEX values. You can go to the VBA Window (I believe) and get the values by going to the IMMEDIATE WINDOW and typing in something like:

?Forms!FormNameHere.ItemNameHere.Backcolor

and then hit ENTER (where ItemNameHere is a valid object which is on the form and the form needs to be open at the time). It should return the color number and not the HEX value.
 
It can use hash too in this way:
Code:
If A>B Then
    Me!Control.BackColor = vbRed
Else
    Me!Control.Backcolor = [COLOR=Blue]Val("&H" & "FFFFFF")[/COLOR]
End If
 
It can use hash too in this way:
Code:
If A>B Then
    Me!Control.BackColor = vbRed
Else
    Me!Control.Backcolor = [COLOR=blue]Val("&H" & "FFFFFF")[/COLOR]
End If

Cool to know.
Thanks.jpg
 

Users who are viewing this thread

Back
Top Bottom