Chapter 7 - Writing Formulas
Crystal Reports gives you the option to program formulas in either Crystal syntax or Basic syntax. This chapter teaches you how to program with both Basic syntax and Crystal syntax. Dozens of code samples show you exactly how to write code that you can put in your own reports today.
Crystal Report Conditional Expressions
When performing actions based upon how a condition evaluates, there are numerous ways to build the condition. For example, when writing an If statement, you can compare a field to a constant using a variety of relational operators and you can join multiple conditions using Boolean operators. This section shows you all the ways you can evaluate fields and variables to see whether they match a certain value or a range of values.You can test against a single constant or variable using the standard relational operators: <, >, <=, >=, =, <>.
You can compare multiple expressions using the Boolean operators And, Or, Not. The And operator takes two expressions and states that both expressions must be true for the condition to return True. The Or operator takes two expressions and states that only one of the two expressions need to be true for it to return True. The Not operator only uses one expression and makes it the opposite. If the expression is true then it returns False. If the expression is false then it returns True.
A few operators that might be new to you are Xor, Eqv and Imp. Eqv is for logical equivalence. It determines when the two expressions are the same. It returns True when both are true or both are false. If they are not the same, it returns False. The syntax is as follows:
exp1 Eqv exp2
Xor is for logical exclusion. It determines when the two expressions are different. It returns True if one is true and the other false. If both expressions are the same (either both are true or both are false), then it returns False. The syntax is as follows:
exp1 Xor exp2
Imp is for logical implication. If the first expression is true, it implies that the second expression will also be true. If the second expression is also true, Imp returns True. If the second expression is false, Imp returns False because it didn't meet what was implied. On the other hand, if the first expression is false, nothing is implied and the result will be always be True. Thus, there is only one instance where Imp ever returns False: when the first expression is true and the second expression is false. All other circumstances return True. The syntax is as follows:
exp1 Imp exp2
The Is operator is used with the Case statement when you want to use the relational operators (e.g. >, <, etc.). An example demonstrating this was already shown in the discussion on the Case statement.The In operator is used for testing if a field or variable exists as an element in an array or if it falls within a range of values. For more information, see the previous sections Array Data Types and Range Data Types.
Chapter 1 discussed the Three-Pass Processing Model. Knowing how this model works is especially important when writing formulas. The type of formula determines when it is processed. Where you place a formula on a report and the functionality within that formula also affects when the formula is evaluated and whether it returns the expected value or not. A formula can be placed on any section of your report, but you should plan this in advance so that you can guarantee the proper results. To determine where to place a formula, you need to know the rules that Crystal Reports uses to evaluate functions. The following list shows the rules that are applied.
- A formula that only references variables (it doesn't use database fields or group/summary fields) is evaluated before any records are read. It is only evaluated one time and its value is saved for the duration of the report.
- Formulas using database fields are evaluated while records are being read. It is re-evaluated every time a new record is read from the data source.
- Formulas using group fields, summary fields, or page related fields (e.g. the page number) are evaluated after the records are read and while the report is being printed.
- When there are multiple formulas within a section, you can't determine which formula will be called first. This is because formulas within the same section are not evaluated in any particular order.
There are times when the default rules listed above will not give you the results you desire. For example, there may be a situation where you have two formulas in the same section and one formula relies upon the other to be called first. According to the default rules listed above, you know that there is no way to determine which one will be called first. You need some way to force one formula to be called before the other. Fortunately, Crystal Reports lets you override the default rules to fit your particular situation. There are four keywords that let you set when a formula will be evaluated: BeforePrintingRecords, WhileReadingRecords, WhilePrintingRecords, and EvaluateAfter. The keyword is added to the beginning of a formula to force it to be evaluated at a time that is different than its default behavior. These keywords are listed in Table 7-4.
Table 7-4. Evaluation Time Keywords
| Evaluation Time | Description |
|---|---|
| BeforeReadingRecords | Evaluate the formula before any database records are read. |
| WhileReadingRecords | Evaluate the formula while reading the database records. |
| WhilePrintingRecords | Evaluate the formula while printing the database records. |
| EvaluateAfter(formula) | Evaluate after another function has been evaluated. |
You can cut down on the amount of time spent debugging formulas if you start out by following some general guidelines. The remainder of this section gives guidelines and examples for you to consider when writing your formulas. If you're having problems with getting a formula to work, then you should come back to this section as a refresher and see if any of these guidelines would help you.
To read all my books online, click here for the Crystal Reports ebooks.

Become a Crystal Reports expert with the authoritative resource available.
The tuturials and tips in this book will take your skills to the next level.