The result of a formula, or the value that is printed when the formula is placed in a report, is called the value returned by the formula. Every formula in Crystal Reports must return a value. Basic syntax does this by setting the value of the special variable formula. For example, here is a simple Basic syntax formula that returns the value 10:
formula = 10
The value returned by a formula can be one of the seven simple data types supported: Number, Currency, String, Boolean, Date, Time and DateTime. Crystal Reports also supports range types and array types, but these cannot be returned by a formula.
If the variable formula is not assigned a value, it is not a complete Basic syntax formula.
Sometimes you may want to write a formula that just declares and initializes some global variables. These formulas are commonly inserted into the report header section of a report. In such cases, assign any value to the special variable formula. Every formula must return a value, even if you are not interested in using that value.
Example
Rem Some Global variable declarations Rem Remember to set the value of 'formula' Global x As String, y As Number, z As DateTime x = "hello" y = 10.5 z = #Aug 6, 1976# formula = 10
The formula variable can be set several times within a single formula. For example, suppose a company has a shipping policy in which orders over $1,000 are insured, but orders below that amount are not insured:
Rem A formula that returns a String value If {Orders.Order Amount} >= 1000 Then formula = "Insured shipping" Else formula = "Regular shipping" End If
The above formula returns the text string value "Insured shipping" if the value of the database field {Orders.Order Amount} is greater than or equal to 1000; otherwise, it returns the text string value "Regular Shipping." Notice that the formula variable appears twice in the above example.
If the formula variable is set to a value of one type, it cannot be set to a value of another type later in the same formula. For example, replacing the String "Regular Shipping" in the above example with the Number 10 would result in an error since the special variable formula was first set to the String value "Insured shipping."
The reason for this restriction is that Crystal Reports needs to know in advance what the return type of a formula will be so that it can allocate enough storage for the returned values. This is because different types have different storage requirements. Another reason is that the formatting options available for a formula field depend on its type. For example, a Number field has Number formatting options, such as the number of decimals to display, which do not make sense for a String field.
Note The special variable formula should not be declared, unlike other variables used in a Basic syntax formula.
Basic syntax is not case-sensitive. What this means is that formula, Formula, and FORMULA are all considered to be the same. This is true of all variable names, functions, and keywords used in a Basic syntax formula.
Note The only exception to this rule is for strings. The string "Hello" is not the same as the string "hello".
Many of the examples in this section refer to the Xtreme sample database, which can be downloaded from the Java Developer Zone. To download the Xtreme sample database, go to http://www.businessobjects.com/products/dev_zone/java/default.asp.
Comparing Basic Syntax Formulas with Functions in Visual Basic | Variables