Changing Font Color Based on Value

jcruzAME

Registered User.
Local time
Today, 02:18
Joined
Oct 5, 2011
Messages
135
If I want to make the font color red (forecolor) red if the value of the text box is a negative number, what's the VB Code?

I tried If text.value < 0 Then
text.forecolor = vbRed

. . . that makes the whole box highlight red. Is there a way to make JUST the font turn a color?

If so, what's the correct way to write it, since "red" and #ED1C24 don't work?
 
I have just tried this (Access 2010) and it works. The only difference is I declared as below:

If Me.txtTitle.Value < 0 Then

Me.txtTitle.ForeColor = vbRed

Else

Me.txtTitle.ForeColor = vbBlack

End If
 
jCcruzAME, look into Conditional Formatting.
 
I've always found conditional formatting to be temperamental. It either doesn't work or it corrupts my form so I always code it.

I now create a function that I can pass a control value to, a case statement then selects what colour it needs to be.
 
I've always found conditional formatting to be temperamental. It either doesn't work or it corrupts my form so I always code it.
It all depends on how it's written and how you're referencing. Conditional Formatting will do just fine for a small matter such as this.
 
In access report, I'm trying to change color of text in specific records based on the true/false value in another record. Works in forms using conditional formatting, but won't seem to work in a report. Here's what works in forms:
IIf([2009 Symposium]=true, forecolor=255 ....this changes the records to red.

But using the same expression in are report doesn't change the text color. Any thoughts.
 
I've always found conditional formatting to be temperamental. It either doesn't work or it corrupts my form so I always code it.

I now create a function that I can pass a control value to, a case statement then selects what colour it needs to be.

Everybody's habits are guided by their experience, but in well over a decade I've never had a problem with Conditional Formatting, on a simple thing like this, causing problems, much less corruption.

And CF, of course, will work with Datasheet or Continuous View Forms, while code, in most cases, won't.

But changing colors when a Control's data is a negative number can be done without either:

In the Format Property box

To show in Red only, i.e. 5

0;0[Red]

To show in Red with Negative Sign, i.e.-5

0;[Red]

To show in Red and inside of parenthesis, i.e. (5)

0;(0)[Red]

Linq ;0)>
 

Users who are viewing this thread

Back
Top Bottom