Avatar of SteveL13
SteveL13
Flag 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?
Microsoft Access

Avatar of undefined
Last Comment
Dale Fye

8/22/2022 - Mon
Dale Fye

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

Mark Edwards

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.
SteveL13

ASKER
Am getting "type mismatch" with both suggestions.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Mark Edwards

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.
SteveL13

ASKER
Sorry.  I don't understand.
ASKER CERTIFIED SOLUTION
Mark Edwards

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Dale Fye

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.