Tuesday, December 14, 2010

Conditional Formatting in Crystal Reports

What exactly is Conditional Formatting in Crystal Reports?

Conditional formatting, put simply, is formatting  (e.g, font color, font style, etc.) applied to data that meets certain rules or conditions.  For example, the person requesting the report would like the Total Sales field to have a green background if the amount is greater than $5000.

But how is this conditional formatting magic done?

Select the field to be formatted.Right-click and choose the border tab – background color is in the center of that tab.Do not click in the color checkbox, rather click on the X+2 box directly to the right of the background color. The Formula Editor will open.In the Operators column choose Control Structures for the If Then Else.Double-click to bring that down to the work area. At the top of the functions column, you’ll see two new categories associated with formatting text/numbers: Formatting FunctionsColor ConstantsNow the tricky part: you have to construct a formula. I’ve included a couple of examples below.if Sum ({Orders.Order Amount}, {@GroupSort})>5000 then crGreen else crNoColor

Note that the colors are preceded by cr in lower case – Crystal Reports needs this, so do not enter a color name without it.  Also note that the else argument is followed by crNoColor.  The else could also be followed by DefaultAttribute, like so:

if Sum ({Orders.Order Amount}, {@GroupSort})>5000 then crGreen else DefaultAttribute

The results would be the same but the second formula would revert back to whatever the formatting color was that did not meet the condition. Of course, the formulas could be much more complex but the logic remains the same.  If the formula returns true do this othewise do something else.

Happy formatting!

We cover conditional formatting in our Introduction to Crystal Reports class.


View the original article here

No comments:

Post a Comment