Change forecolor of text box depending on query field

SteveL13
SteveL13 used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dale FyeOwner, Dev-Soln LLC
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

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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Mark EdwardsChief Technology Officer

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
Commented:
The way you set the forecolor is with a constant (i.e. vbBlue, vbYellow, etc.), an RGB() function (RGB(125,125,125)) or a hex value (which I never use.)

The values in your Forecolor field are text.  You can't set a forecolor with a text value, so you'll need something like:
Select Case me.Forecolor
  Case "Blue"
	Me.txtControlname.forecolor=vbBlue
  Case “Yellow”
	Me.txtControlname.forecolor=vbYellow
‘….etc.
End Select

Open in new window

Daniel PineaultPresident / Owner CARDA Consultants Inc.
Distinguished Expert 2018
Commented:
If ever you don't like the preset vb colors, they can be a little harsh, you can use a case statement just like Mark suggests, but instead specify the rgb color you want
Me.txtControlname.forecolor = RGB(87,171,83)

Open in new window

this will allow you to get whatever color you desire.

Another alternative is to use HTML colors and for that you would do something more like
Me.txtControlname.forecolor = Val("&H" & "FFFFFF")

Open in new window


Either of the above will give you much more control on the exact color you want, instead of just a few preset ones.  You can use google for HTML and RGB color charts to make your life easier to determine the values you might wish to use if you are not already familiar with these color standards.
Dale FyeOwner, Dev-Soln LLC
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial