How to change color on text using IF

PoorCadaver

Registered User.
Local time
Today, 13:26
Joined
Oct 25, 2011
Messages
30
Hi!

I would like to change color on the text placed in a textbox depending on the value. The value comes from a query connected to a table.

Something like:

in textbox: IF Me.ID = 30 THEN color = #FF0000
ELSEIF Me.ID = 40 THEN color = #00FF00
ELSE color = #000000

Can't figure it out by myself =(

Thanks!
 
To set the font colour of a text box you will need to use;
Code:
Me.ControlName.ForeColor = #FF0000

So your code look something like;
Code:
IF Me.ID = 30 THEN Me.ID.ForeColor = #FF0000
ELSEIF Me.ID = 40 THEN Me.ID.ForeColor = #00FF00
ELSE Me.ID.ForeColor = #000000
 
Hmm, I can't really get this to work...
This is what I have now:

Code:
Private Sub SevicePack_BeforeUpdate(Cancel As Integer)
    If Me.ServicePack = 3 Then Me.ServicePack.ForeColor = "#FF0000"
    ElseIf Me.ServicePack = 2 Then Me.ServicePack.ForeColor = "#00FF00"
    Else: Me.ServicePack.ForeColor = "#000000"
End Sub

Whent the column ServicePack = 3 I want one color, and when it is 2 I want another.
 
Try the After Update event.

Also here's a little trick for you. If you want to check what value a control is holding when you run a piece of code insert;
Code:
MsgBox "This Controls holds " & Nz(Me.YourControlName, "Null Value")
 
... oh yes you will also need that code in the Form's On Current event as well.
 
Try it this way and see if works, I think HTML colours are a little tricky to handle in access.


If Me.ServicePack = 3 Then
Me.ServicePack.ForeColor = 255
ElseIf Me.ServicePack = 2 Then
Me.ServicePack.ForeColor = 65280
Else: Me.ServicePack.ForeColor = 0
End If

SmallTime
 
When It's compiling, this error shows:

Can't find the method or data member (but in swedish)

When I type Me.ServicePack. - then I can't chose ForeColor, only Value apperas in the drop down...
 
Not really, there is some confidental material that I don't want to share. But I can make a copy and remove it.
 
Copy ONLY the relevant forms\tables\queries into a new database and post that.

Also, I take it the textbox in question is named 'ServicePack' and not something like 'textbox1'


SmallTime
 
Here it is!


SPELLING ERRROR Alert!

The Property Definition for the 'ServicePack' Control has its Name spelled as 'SrvicePack' (by mistake, I presume). Fix that and the problem becomes what color do you really want the Text to be, or perhaps do you want to set the 'Background' Property as well.
 
OMG how annoying!

Now it almost works =) There is just one thing, the color of ALL posts change, no matter what the value. And I need to click on the post for it to change from black to red...
 
Here's the working file

It's a continuous form for you have to use conditional formatting.

SmallTime
 

Attachments

Is it working OK?

Take a look at conditional formatting, In design view right click the text box and select Conditional Formatting to see how it works.

You can't use code to change individual fields in a continuous form.


Take Care
SmallTime
 
Oh, I see! That's what I was looking for! =)
But with this you are able to mar the content of the textbox. The other textboxes ar set up so that they can not be marked by the cursor, and I can't get it right with this formatting...
 
Oh, I see! That's what I was looking for! =)
But with this you are able to mar the content of the textbox. The other textboxes ar set up so that they can not be marked by the cursor, and I can't get it right with this formatting...

Solved it ;-) Disabled with white background.

Thanks a lot for the help! Now I can continue my work again =)
 

Users who are viewing this thread

Back
Top Bottom