Link to home
Create AccountLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

Change forecolor of text box depending on query field

I have a sub-report that has one text box.  The control source for the text box comes from a query.  The query includes a field named ForeColor.  When the sub-report prints I want the color of the text being printed to be the same as ForeColor.  So for example, if the query ForeColor indicates Blue, I want the textbox to print blue.  Or if the query ForeColor indicates Red, I want the textbox to print red.  

Make sense and how do I do this with VBA code?
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

In the detail sections Format event, use code similar to:

Private Sub Detail_Format

     me.txtFieldName.ForeColor = me!ForeColor

End Sub

Open in new window

Be aware that simply the name of the color won't do it.  You need to add "vb" to the beginning of your forecolor text value.

Private Sub Detail_Format
     me.txtFieldName.ForeColor = “vb” & me!ForeColor
End Sub

Open in new window

i.e. "vbBlue", not just "Blue"  (I tested it)

Also, the color shade you get might not be the one you want.  You'll need to be willing to put up with some funky shades.  If you don't like them, you'll need to build a code process that gives you the color you want for the forecolor text value.

Also be aware that there might not be a "vb" color constant for all of the values in the forecolor field.
Avatar of SteveL13

ASKER

Am getting "type mismatch" with both suggestions.
Sorry.  The constant is vbBlue, not "vbBlue"  You'll need to create a code process to provide the proper forecolor constant or RGB() value for the value in the Forecolor field.
Sorry.  I don't understand.
ASKER CERTIFIED SOLUTION
Avatar of Mark Edwards
Mark Edwards
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Steve,

what values are you storing in the ForeColor field of your table?  if they are the results of an RGB function call, then the code I gave you should work.  But it would be helpful to know the name of the control you are attempting to update, and possibly give us a couple of records of data, so that we can see what you are working with.

Dale