Search code examples
crystal-reportscrystal-reports-2008

how to change font color based off database result


In my report, I'd like to change the font color of a text box (text box contains a circle wingdings character, different colors will be used for different statuses). I have a field in my database that has one of 5 options - r,y,b,g, or o for red, yellow, blue, green or orange. Is there a way based on this result for the item to change the color of the textbox to match the corresponding color for each item in the report? Thank you, I am new to crystal reports.

Edit- I tried to right click on the text box where I have placed the circle and then went to formatting. I don't see the formula box like I do when I click on a DB field. Any way around this? I need the colored circle, not just a circle in every item and it needs to relate to that color from the DB field.


Solution

  • Yes:

    • Right-click on your text box and select Format Field... .
    • Select the Font tab in the Format Editor dialog.
    • Click on the x-2 button to the right of the selected Color option (which will default to black).
    • In the Format Formula Editor, enter a formula like:

    if {myTable.ColourOption} = "r" then crRed else 
    if {myTable.ColourOption} = "y" then crYellow else 
    if {myTable.ColourOption} = "g" then crGreen else 
    if {myTable.ColourOption} = "b" then crBlue else 
    if {myTable.ColourOption} = "o" then Color (255, 128, 0)
    

    The function pane of the Format Formula Editor should include a Color Constants node - this will list available predefined Crystal colours, or you can define your own using the Color (r, g, b) function.

    • Click on the x-2 check button (or press Alt and C) to check whether the entered formula is valid - if it is, click the Save and close button at the top left of the Format Formula Editor, then click on the OK button at the bottom of the Format Editor dialog.

    If you now preview the report, you should find that your text box is coloured based on the value of your colour option field.