We help IT Professionals succeed at work.

Change forecolor of text box depending on query field

SteveL13
SteveL13 asked
on
67 Views
Last Modified: 2019-01-08
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?
Comment
Watch Question

Dale FyeOwner, Dev-Soln LLC
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2010

Commented:
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 EdwardsChief Technology Officer
CERTIFIED EXPERT

Commented:
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.

Author

Commented:
Am getting "type mismatch" with both suggestions.
Mark EdwardsChief Technology Officer
CERTIFIED EXPERT

Commented:
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.

Author

Commented:
Sorry.  I don't understand.
Chief Technology Officer
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Daniel PineaultPresident / Owner CARDA Consultants Inc.
CERTIFIED EXPERT
Distinguished Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Dale FyeOwner, Dev-Soln LLC
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2010

Commented:
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