[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

Need a VB code to change font color of a text based based on value

This is in access, and I have a text box which looks up a certain value for a record, and the value will either be a 1 or 2, and then the text box displays some corresponding text.  

Now, I would like to find some code where if the value is a 1, i can make it red, and if it is a 2, i can make the text green.  Can text box text color be changed like that?  This is access 97, so rich text boxs are not allowed (to my knowledge)
0
salival
Asked:
salival
  • 2
  • 2
1 Solution
 
Erick37Commented:
The Forecolor property is what you need to change,

Select case x
Case 1: Text1.ForeColor = vbRed
Case 2: Text1.ForeColor = vbGreen
end Select

Other examples:

Text1.ForeColor = RGB(100, 200, 64)
Text1.Forecolor = &h00FF00
0
 
David LeeCommented:
In Access you can also use Conditional Formatting to accomplish this.  But I don't remember if that was available in Access 97 or not.  Do you need to do this in a report or on a form?
0
 
salivalAuthor Commented:
on a form
0
 
Erick37Commented:
Is the ForeColor property working for you then?
0
 
David LeeCommented:
I believe Conditional Formatting is associated with reports and not forms.  On a form you just need to decide how to trigger the change and then use the code that Erick37 provided.  Since this is on a form I'm guessing you want the change to take affect when the form is displayed initially and each time a new record is shown.  Possibly also if the value of the field changes.  Pick out which events will cover those changes and insert Erick37's code in those events.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now