Change font colour in VB (1 Viewer)

Tracy

Registered User.
Local time
Today, 05:09
Joined
Oct 19, 2001
Messages
71
Howdy,

Does anyone know how to change the font colour in a field on a form or in a report depending on a criteria. So what I want to say is:

If fieldvalue < 0 then

'change font colour to red

End If.

Hope someone can help.
Thanks
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 23:09
Joined
Feb 28, 2001
Messages
27,223
In your vba code, the properties to set are

[control].ForeColor - for the text in a text box, combo box, list box, or label - the foreground color.

[control].BackColor - for the background color of the above items

[control].BorderColor - for the color for the line around the items.

For things like radio buttons and toggle buttons, you won't find all three of those items. Lines, for example, only have a border color. Buttons generally don't have a foreground color. As a side note, if you select a control format such as "shadowed" then the size and color of the shadow can be controlled using the border color and border size properties.

You can set any of these colors to the following constants:

vbBlack
vbRed
vbYellow
vbGreen
vbCyan
vbBlue
vbMagenta
vbWhite

or you can look up the RGB function in the help files to see how to customize any color you wish.

finally, you can look up in the help files to find the System Color Constants. Things like vbDesktop = the color of your desktop. vbMenuBar = the color of your menu's background color. vbActiveBorder = the color of the border on your active window. Etc. etc.

Using the latter constants usually means that your Access application follows the default color scheme of your Desktop setup. Sometimes this even works nicely. However, usually you don't want to mix the Desktop settings and custom settings because you could get some really PUTRID combinations as a result. Some folks have the darnedest color schemes I've ever seen. So be careful about using these schemes unless you want to get some really nasty comments from folks with any sense of color at all.

Now as to WHERE to set these properties, you have to decide when they would be set any when/if the setting becomes dynamic. Most folks will either use the Form_Load event (for relatively static settings) or the {control}_GetFocus / _LostFocus events (for relatively dynamic settings.) This is something that MUST be done using VBA if you really want to do your color changes based on criteria. But it is pretty easy. I do it all the time, usually in my case with the _GetFocus / _LostFocus code.
 

Tracy

Registered User.
Local time
Today, 05:09
Joined
Oct 19, 2001
Messages
71
Wow! Thanks v much Doc Man!
 

waf

New member
Local time
Yesterday, 21:09
Joined
Jan 26, 2012
Messages
1
Howdy,

Does anyone know how to change the font colour in a field on a form or in a report depending on a criteria. So what I want to say is:

If fieldvalue < 0 then

'change font colour to red

End If.

Hope someone can help.
Thanks


try this might work

1. select your field
2. do an after update event
3. select the code "VB"
4. type in the following lines

If [fieldvalue] < 0 then
[fieldvalue].forecolor = vbred
else
[fieldvalue].forecolor = (orginal color ) vb+color or rgb(87,171,83)
endif "green"

5. save and try it

two ways to select the colors

1 example "vbred" for red
"vbblue" for blue...
vb+(red,blue, black, green, cyan ...)

2 sample rgb (34,47,123) rgb (red, green, blue)
rgb (R= 0 to 255, g= 0 to 255, b= 0 to 255)
 

Ahmed@soundevo

Registered User.
Local time
Today, 05:09
Joined
Mar 7, 2012
Messages
18
Sorry to bump this but I have a question:

Howdy everyone,

I had a read on this thread w w w.access-programmers.co.uk/forums/showthread.php?t=34499 (please remove the spaces)

which helped me alot! however I'm come across a problem on my side. I've made a database where you input relevant information when a customer comes in store to enquire about products (you enter in their information, what they enquired about and a quotation). Then there is a series of check boxes where if the enquiry has been solved then you tick the box, if the enquiry is on going then you tick the solved box and the on going box, if the enquiry is closed and no further action needed then you tick on the solved, on going and no further action box.

As you move through and check the relevant boxes you press on a button called "process enquiry" (this is where I have the problems). Depending on what check boxes are ticked the colours change, for example red is a no go, yellow is a maybe and green is a go.

I've coded the button to change the boxes based on what is ticked, (if all 3 are ticked then red, if two are ticked then yellow and if one is ticked then green).

Now the problem I have is that the colours change for every single record in my form. So if record 2 is yellow and record 3 and green then both of them change to the same colour. How can I change it so each record changes independetly based on what is ticked ?

Here is how my code looks below (I've included some of it, not all of it)

Code:
Private Sub Process_Enquiry_Click()
If Tickbox_Actioned = True And Tickbox_ChasedUp = True And Tickbox_Forget = True Then [EnquiryBox].ForeColor = vbRed Else If Tickbox_Actioned = True And Tickbox_ChasedUp = True Then [EnquiryBox].ForeColor = vbYellow Else If Tickbox_Actioned = True Then [EnquiryBox].ForeColor = vbGreen Else [EnquiryBox].ForeColor = vbWhite
If Tickbox_Actioned = True And Tickbox_ChasedUp = True And Tickbox_Forget = True Then [QuotationBox].ForeColor = vbRed Else If Tickbox_Actioned = True And Tickbox_ChasedUp = True Then [QuotationBox].ForeColor = vbYellow Else If Tickbox_Actioned = True Then [QuotationBox].ForeColor = vbGreen Else [QuotationBox].ForeColor = vbWhite
End
End
End Sub

If required I can put up a picture of the form

My question to you guys is how can i code the button so that when i click on it, it changes each record individually and not every single one.

Thank you for any help!
 

nvm373xx.Ph

New member
Local time
Yesterday, 21:09
Joined
Jan 19, 2013
Messages
1
try this might work

1. select your field
2. do an after update event
3. select the code "VB"
4. type in the following lines

If [fieldvalue] < 0 then
[fieldvalue].forecolor = vbred
else
[fieldvalue].forecolor = (orginal color ) vb+color or rgb(87,171,83)
endif "green"

5. save and try it

two ways to select the colors

1 example "vbred" for red
"vbblue" for blue...
vb+(red,blue, black, green, cyan ...)

2 sample rgb (34,47,123) rgb (red, green, blue)
rgb (R= 0 to 255, g= 0 to 255, b= 0 to 255)

________________________________________________________________

Changing the forecolor of the label/textbox inside the body of the condition is the trick....

Yes this was true, For example i want to input an integer and i want to know if the integer i've entered is ODD or EVEN.

The deal is:
if the integer is an ODD, it will display as a RED text in the label,
else if it is EVEN, it will display as blue in the label.

Take this as an example:

Input number: text1
Output: label1

Code:

if (val(text1) mod 2) = 1 then
label1 = "ODD"
label1.Forecolor = vbRed
else
label1 = "EVEN"
label1.Forecolor = vbBlue
end if

*mod is a modulo operator function used to take the remainder of the text1 divided by 2

well that is all...i hope this can help for all the person with the same problem as changing a text displayed according to a conditions/criteria......

-nvm373xx.Ph
_________________________________________________________________
 
Last edited:

sbaud2003

Member
Local time
Today, 09:39
Joined
Apr 5, 2020
Messages
178
In your vba code, the properties to set are

[control].ForeColor - for the text in a text box, combo box, list box, or label - the foreground color.

[control].BackColor - for the background color of the above items

[control].BorderColor - for the color for the line around the items.

For things like radio buttons and toggle buttons, you won't find all three of those items. Lines, for example, only have a border color. Buttons generally don't have a foreground color. As a side note, if you select a control format such as "shadowed" then the size and color of the shadow can be controlled using the border color and border size properties.

You can set any of these colors to the following constants:

vbBlack
vbRed
vbYellow
vbGreen
vbCyan
vbBlue
vbMagenta
vbWhite

or you can look up the RGB function in the help files to see how to customize any color you wish.

finally, you can look up in the help files to find the System Color Constants. Things like vbDesktop = the color of your desktop. vbMenuBar = the color of your menu's background color. vbActiveBorder = the color of the border on your active window. Etc. etc.

Using the latter constants usually means that your Access application follows the default color scheme of your Desktop setup. Sometimes this even works nicely. However, usually you don't want to mix the Desktop settings and custom settings because you could get some really PUTRID combinations as a result. Some folks have the darnedest color schemes I've ever seen. So be careful about using these schemes unless you want to get some really nasty comments from folks with any sense of color at all.

Now as to WHERE to set these properties, you have to decide when they would be set any when/if the setting becomes dynamic. Most folks will either use the Form_Load event (for relatively static settings) or the {control}_GetFocus / _LostFocus events (for relatively dynamic settings.) This is something that MUST be done using VBA if you really want to do your color changes based on criteria. But it is pretty easy. I do it all the time, usually in my case with the _GetFocus / _LostFocus code.
THANK YOU SIR
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 23:09
Joined
Feb 28, 2001
Messages
27,223
It should be noted that another way exists to answer the original question. There IS such a thing as conditional formatting, where you can define a condition and the color scheme that goes with each condition (from TWENTY years ago...).


I'm not going to swear to this, but the conditional formatting options might not have been available back then. But if they were, at the time of my original answer, I didn't know them.

 

GPGeorge

George Hepworth
Local time
Yesterday, 21:09
Joined
Nov 25, 2004
Messages
1,909
It should be noted that another way exists to answer the original question. There IS such a thing as conditional formatting, where you can define a condition and the color scheme that goes with each condition (from TWENTY years ago...).


I'm not going to swear to this, but the conditional formatting options might not have been available back then. But if they were, at the time of my original answer, I didn't know them.

If you can articulate a logical rule to govern highlights, you can use conditional formatting to implement it.
 

Users who are viewing this thread

Top Bottom