Mastering Business Analysis with Crystal Reports 9 (Wordware Applications Library)

In Crystal Reports, you have the option of using either Crystal or Basic syntax.

If you're familiar with Microsoft Visual Basic (or other versions of Basic), then you'll find Basic syntax quite familiar. Basic syntax is similar to Visual Basic, except it includes additional extensions to aid with reports.

In contrast, the Crystal syntax is similar to the Pascal or Delphi programming languages.

Note

Crystal syntax is the default formula language within Crystal Reports. In this book, you utilize Crystal syntax as your formula language. For more information about Basic syntax, check out "Creating Formulas with Basic Syntax" in the Crystal Reports online help.

Both languages are capable of performing the same jobs, and there are no performance issues associated with the languages. Thus, the choice is entirely based on your familiarity with the language. It is equally possible to create any formula within both the Crystal and Basic syntaxes.

No matter which language you choose to use, there are some fundamental concepts to formula creation found in both languages. The following sections summarize the most important areas of the language — enough to get your formulas up and running.

Common Operators

In the world of programming, operators provide some type of calculation on two values, resulting in the return of a single value.

Although Crystal Reports contains a plethora of operators that you may use within your formulas, there are essentially four basic operators you'll use over and over within your reports. These are summarized in the following table.

Table 10-1: The basic arithmetic operators used within report formulas

Operator

Operation

Description

+

Add

Returns the sum of the values within the formula.

Subtract

Returns the remainder of deducting one value from another.

*

Multiply

Returns the product of multiplying values together.

/

Divide

Returns the quotient of dividing one value by another.

Reserved Characters

Several keyboard characters are used by the Crystal and Basic languages and are thus considered reserved. The following table summarizes the different uses of these characters within report formulas.

Table 10-2: Reserved characters of the Crystal and Basic languages

Reserved Character

Name

Description

{ }

Curly brackets

Used to contain the report objects (such as the database or formula fields). For example: {Employee.First Name}

[ ]

Square brackets

Used to create computational formulas. Another common use for them is to return the character of a string field. For example: {Employee.First Name}[1] returns the first character of this string field.

( )

Parentheses

Used with operators to contain the different parts of a formula and define the order in which the calculations should take place.

.

Period

Separates the table name from the database field. For example: {Employee.Last Name}.

" "

Quotation marks

Used to surround text that you wish to display within your formulas. For example, "USA" would display the text USA within your report. We look at adding text to your report formulas later in this chapter.

'

Comma

Used to separate multiple arguments within a formula. Arguments are values that exist within a formula. See page 202 for examples of arguments in your formulas.

@

Formula

Designates formula fields. For example: {@Name_of_Formula}

?

Parameter

Designates parameter fields. For example: {?Name_of_Parameter)

#

Running total

Designates running total field. For example: {#Name of Running Total}

Summary

Designates a summary field. For example: {Name_of_Summary_Field}

%

SQL

Expression

Designates an SQL Expression field. For example: {%Name_of_SQL_Expression}

Syntax Differences between Crystal and Basic

Unfortunately, there are a couple of syntax differences between Crystal and Basic. These differences are summarized in the following table.

Table 10-3: Syntax differences between Crystal and Basic

Reserved Character (Crystal)

Reserved Character (Basic)

Name

Description

//

'

Comment

Designates a comment. Comments are text ignored by the formulas and used to help explain the code's functionality.

;

:

Semicolon

Allows you to separate multiple statements within a single formula.

Enter key

-

(space followed by underscore)

New Line

Designates a new line within a formula. A new line may only start between a field or function and an operator.

Категории