Basic Vs Crystal Syntax
Basic Vs Crystal Syntax
Overview
Crystal Reports version 8 and higher offers two different syntaxes for writing
formulas: the Crystal syntax and the Basic syntax.
This paper describes some of the differences between the two syntaxes and
provides general information on the control structures that are available for the
syntaxes.
Contents
INTRODUCTION ............................................................................................ 3
FORMULA COMMENTS ................................................................................. 3
Crystal syntax .................................................................................................... 3
Basic syntax....................................................................................................... 3
THE RESULT OF A FORMULA........................................................................ 4
Formula Variable ........................................................................................4
Crystal syntax .................................................................................................... 4
Basic syntax....................................................................................................... 4
Output of the Formula .................................................................................4
Crystal syntax .................................................................................................... 4
Basic syntax....................................................................................................... 4
ASSIGNMENT OPERATOR ............................................................................. 5
Crystal syntax .................................................................................................... 5
Basic syntax....................................................................................................... 5
STATEMENTS .............................................................................................. 5
Separating Statements..................................................................................5
Crystal syntax .................................................................................................... 5
Basic syntax....................................................................................................... 5
Continuing Statements .................................................................................6
Crystal syntax .................................................................................................... 6
Basic syntax....................................................................................................... 6
USING DATE, TIME, OR DATETIME VALUES .................................................. 6
Crystal syntax .................................................................................................... 6
Basic syntax....................................................................................................... 7
EXTRACTING ELEMENTS FROM A STRING ..................................................... 7
Crystal syntax .................................................................................................... 7
Basic syntax....................................................................................................... 7
ARRAYS ..................................................................................................... 7
12/28/2004 12:12 PM Copyright © 2004 Business Objects. All rights reserved. Page 1
Crystal Reports Crystal versus Basic Formula syntax
Declaring an Array......................................................................................8
Crystal syntax .................................................................................................... 8
Basic syntax....................................................................................................... 8
Referencing Elements within an Array ........................................................8
Crystal syntax .................................................................................................... 8
Basic syntax....................................................................................................... 8
Assigning values to an array........................................................................8
Crystal syntax .................................................................................................... 8
Basic syntax....................................................................................................... 8
VARIABLES ................................................................................................. 8
Variable Scopes ...........................................................................................9
Crystal syntax .................................................................................................... 9
Basic syntax....................................................................................................... 9
Variable Types ...........................................................................................10
Crystal syntax .................................................................................................. 10
Basic syntax..................................................................................................... 10
Variable Names..........................................................................................11
Declaring Multiple Variables in One Statement ........................................11
Crystal syntax .................................................................................................. 11
Basic syntax..................................................................................................... 11
IF-THEN-ELSE CONTROL STRUCTURES ...................................................... 11
Else If versus ElseIf....................................................................................12
Crystal syntax .................................................................................................. 12
Basic syntax..................................................................................................... 12
Single-Line IF Statements ..........................................................................12
Crystal syntax .................................................................................................. 12
Basic syntax..................................................................................................... 13
Multi-Line IF Statements ...........................................................................13
Crystal syntax .................................................................................................. 13
Basic syntax..................................................................................................... 13
SELECT OR CASE STATEMENTS ................................................................. 14
Crystal syntax .................................................................................................. 14
Basic syntax..................................................................................................... 14
LOOPING .................................................................................................. 14
For/Next Loops ..........................................................................................15
Crystal syntax .................................................................................................. 15
Basic syntax..................................................................................................... 15
Exiting a For/Next loop .............................................................................16
Crystal syntax .................................................................................................. 16
Basic syntax..................................................................................................... 16
Do Loops....................................................................................................16
Do … While..................................................................................................... 17
Crystal syntax only .......................................................................................... 17
Do While … Loop and Do Until … Loop ....................................................... 17
Basic syntax only............................................................................................. 17
Do … Loop While and Do … Loop Until ....................................................... 18
Basic syntax only............................................................................................. 18
While Loops ...............................................................................................18
Crystal syntax .................................................................................................. 19
Basic syntax..................................................................................................... 19
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
Introduction
Most formulas in Crystal Reports can be written in either Crystal or Basic
syntax. However, Record Selection and Group Selection formulas must be
written in Crystal syntax.
Crystal syntax is similar to formula writing in C++ while Basic syntax is similar
to formula writing in Microsoft Visual Basic.
Formula Comments
Formula comments are notes included with a formula to explain its design and
operation. Comments do not print and they do not affect the formula; they
appear only in the Formula Workshop. Use comments to explain the purpose of
a formula or explain the steps involved in writing it.
Crystal syntax uses two forward slashes (//) to denote a comment. Comments
can begin on a new line or on the same line as the rest of the code.
A comment beginning with an apostrophe can begin on the same line as the rest
of the code without needing to be separated by a colon.
Crystal syntax
Basic syntax
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
• Basic syntax uses a special variable called formula whereas Crystal syntax
does not.
• Basic syntax returns the value assigned to the formula variable whereas
Crystal syntax returns the last line within the formula.
Formula Variable
Basic syntax requires a special variable called formula to return the value of the
formula whereas Crystal syntax does not.
Crystal syntax
// This formula will output "Hello World"
"Hello World"
Basic syntax
' This formula will output "Hello World"
Formula = "Hello World"
Crystal syntax
// This formula will output the number 5
Numbervar x;
"Hello World";
x :=5
Basic syntax
' This formula will output "Hello World"
Dim x as number
Formula = “Hello World”
x = 5
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
Assignment Operator
The assignment operator is the colon equal sign (:=) in Crystal syntax as
opposed to the equal sign (=) in Basic syntax.
Crystal syntax
Numbervar x;
x := 5; // Notice the colon equal sign versus the equal
// sign
x;
Basic syntax
dim x as number
x = 5 ' Notice the equal sign versus the colon equal sign
formula = x
Statements
All formulas exist as a sequence of statements. For a formula to evaluate
correctly, these statements must be separated and continued correctly.
Separating Statements
In Crystal syntax a semicolon must separate each statement (;). In Basic syntax,
either a new line or a colon must separate statements (:).
Crystal syntax
// A semicolon separates the statements
Numbervar ID;
if {Customer.Last Year's Sales} > 10000 then ID :=
{Customer.Customer ID};
Basic syntax
' A line break separates the statements
dim ID as number
if {Customer.Last Year's Sales} > 10000 then ID =
{Customer.Customer ID}
formula = ID
<OR>
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
Continuing Statements
In Crystal syntax you can continue a statement onto the next line by simply
starting the text on the next line. In Basic syntax you must use the line
continuation character, a space followed by an underscore ( _).
Crystal syntax
// Statement is continued by using a line break
if {Customer.Last Year's Sales} > 10000
and {Customer.Country} = "USA"
then "Good sales"
Basic syntax
' Statement is continued using the underscore
if {Customer.Last Year's Sales} > 10000 _
and {Customer.Country} = "USA" then
formula = "Good sales"
end if
Crystal syntax
Date(2000,01,01); // Crystal syntax only
Time(13,30,50); // Crystal syntax only
DateTime(2000,01,01,13,30,50); // Crystal syntax only
CDate(2000,01,01);
CTime(13,30,50);
CDateTime(2000,01,01,13,30,50);
DateValue(2000,01,01);
TimeValue(13,30,50);
DateTimeValue(2000,01,01,13,30,50);
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
Basic syntax
Formula = CDate(2000,01,01)
Formula = CTime(13,30,50)
Formula = CDateTime(2000,01,01,13,30,50)
Formula = DateValue(2000,01,01)
Formula = TimeValue(13,30,50)
Formula = DateTimeValue(2000,01,01,13,30,50)
In Crystal syntax this is done using square brackets [ ] while Basic syntax uses
parentheses ( ). Negative values specify a position starting from the end of the
string.
Crystal syntax
"HELLO" [5] // Returns "O"
"HELLO" [-3] // Returns "L"
"HELLO" [3 to 4] // Returns "LL"
"HELLO" [-3 to 2] // Returns "EL"
Basic syntax
formula = "HELLO" (5) ' Returns "O"
formula = "HELLO" (-3) ' Returns "L"
formula = "HELLO" (3 to 4) ' Returns "LL"
formula = "HELLO" (-3 to 2) ' Returns "EL"
Arrays
When using arrays, the main differences between the two syntax types are:
• Declaring an array
• Referencing elements within an array
• Assigning values to an array
In both syntaxes the result of a formula cannot be an array. The formulas shown
in the “Declaring an array” and “Assigning values to an array” sections require
additional syntax to ensure the result is a single value and not an array,
otherwise the formula will return an error.
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
Declaring an Array
Crystal syntax uses the Array function to declare an array while Basic syntax
uses parenthesis instead.
Crystal syntax
StringVar Array MyArray; // Notice the Array function
Basic syntax
Dim MyArray() as String ' Notice the parenthesis
Crystal syntax
MyArray[3] // Notice the square bracket
Basic syntax
formula = MyArray(3) ' Notice the parenthesis
Crystal syntax
MyArray := ["a", "b", "c"] // Notice no Array function
Basic syntax
MyArrray = Array("a", "b", "c") ' Notice the Array function
Variables
A variable represents a specific data item or value, and acts as a placeholder for
that value. When a formula encounters a variable, the formula searches for the
value of the variable and uses it in the formula. Unlike a constant value, which is
fixed and unchanging, a variable can be repeatedly assigned different values.
When you assign a value to a variable, the variable maintains the value until you
later assign a new value. Because of this flexibility, it is necessary for you to
declare variables before you use them so that Crystal Reports is aware of them
and understands how you intend to use them. There are three elements involved
when declaring variables:
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
• Scope
• Type
• Name
Variable Scopes
There are three types of variable scopes:
• Local
• Global
• Shared
You can use these three types of variable scopes for both Crystal and Basic
syntax. However, Crystal syntax does not contain the Dim local scope whereas
Basic syntax does.
• Dim or Local – Dim (Basic syntax only) or Local variables are only
recognized within the formula in which they are declared.
• Global – Global variables use the same memory block to store a value for
access throughout a single report object, whether that is the main report or a
subreport.
• Shared – Shared variables are recognized in all report objects within a
single Crystal Reports file. Shared variables are frequently used to pass
data from subreports to the main report.
Crystal syntax
// Variable scope is declared first, then variable type,
// then variable name
Local CurrencyVar C;
Global NumberVar N;
Shared StringVar S;
Basic syntax
' Variable scope is declared first, then variable name,
_ then variable type
Dim B as Boolean
Local C as Currency
Global N as Number
Shared S as String
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
Variable Types
Variable types can be declared in multiple ways:
Number Boolean
Currency Date
String Time
DateTime
Crystal syntax requires that the variable type be specified when declaring the
variable. In Basic syntax if the type is not declared then it will be set when the
variable is first used.
Crystal syntax
// Note that the type is declared before the variable is
// used
Global StringVar s;
s := “Hello”;
s;
Basic syntax
' Note that the type is not declared until the variable is
_ used
Global S
S = “Hello”
formula = S
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
Variable Names
When you declare a variable, you also specify its name. A variable cannot have
the same name as any function, operator or other keyword that is valid for the
syntax used in the formula.
In the Formula Workshop the names of the built-in functions, operators and
other keywords are displayed in a different color so it is easy to see if the
variable name conflicts.
Crystal syntax
Basic syntax
' Note that X, Y, and Z are declared in a single statement
Local X, Y, Z
X = 5
Y = "hello"
Z = True
formula = X
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
Crystal syntax
If {Customer.Last Year's Sales} < 100 Then
"Under $100"
Else If {Customer.Last Year's Sales} in 100 to 200 Then
"Between $100 and $200"
Basic syntax
If {Customer.Last Year's Sales} < 100 Then
formula = "Under $100"
ElseIf {Customer.Last Year's Sales} in 100 to 200 Then
formula = "Between $100 and $200"
End If
Single-Line IF Statements
In Basic syntax, a single-line IF statement does not start a new line after the first
THEN statement.
Line continuation characters such as the underscore (_) can be used to stretch a
single-line IF statement over multiple lines. Starting a new line after the first
THEN makes an IF statement into a multi-line IF statement.
Crystal syntax
// One line
StringVar x
If {Customer.Customer ID} = 1 then x := "One";
x;
<OR>
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
Basic syntax
' One line
Dim x as string
If {Customer.Customer ID} = 1 then x = "One"
Formula = x
<OR>
Multi-Line IF Statements
More than one statement can be executed as the result of a condition being met.
However, in Crystal syntax the two results are enclosed within parentheses
whereas Basic syntax encloses the results within the condition using the ElseIf,
Else, or End If statements.
Crystal syntax
NumberVar x;
NumberVar y;
If {Customer.Customer ID} = 1 then
(
x := 1;
y := 2
); // Notice the parentheses as opposed to End If
x;
Basic syntax
shared x as number
If {Customer.Customer ID} = 1 then
x = {Customer.Customer ID}
formula = "One"
End If ' Notice the End If as opposed to parenthesis
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
Crystal syntax
Select {Customer.Customer ID} // Notice the Select versus
// Select Case
Case 1 : "One" // Notice the colon versus line break
Case 2 : "Two"
Default : "Other" // Notice Default versus Case Else
;; // Notice the two semicolons versus the End Select
Basic syntax
Select Case {Customer.Customer ID} ' Notice the Select Case
_ versus Select
Case 1 ' Notice the line break versus colon
formula = "One"
Case 2
formula = "Two"
Case Else ' Notice the Case Else versus Default
formula = "Other"
End Select ' Notice the End Select versus two semicolons
Looping
Loops enable you to evaluate a sequence of statements multiple times.
Any one evaluation of a formula can have at most 29,999 loop condition
evaluations. Therefore, if a loop structure performs a loop 30,000 or more
times, the following error message appears:
“A loop was evaluated more than the maximum number of times allowed”.
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
For/Next Loops
For loops require a For Counter variable to count the number of iterations
through the loop. If you add a Step statement to the For loop you can control the
increment that the For Counter uses. It can be a negative step number if you
wish to have the loop go from a large number to a small number.
For loops will support an Exit statement. The main differences between Crystal
and Basic syntax are:
• Crystal syntax uses the Do statement to begin the contents of the For loop
whereas Basic syntax does not.
• Crystal syntax uses parenthesis around the contents of the For loop while
Basic syntax encloses the contents of the For loop by ending it with the
Next <Counter variable> statement.
Crystal syntax
// The following formula will return “abc”
Numbervar x;
StringVar Array MyArray := ["a","b","c"];
StringVar Concatenate;
For x := 1 To count(MyArray) Do // Notice the Do statement
(
Concatenate := Concatenate + MyArray[x]
); // Notice the parenthesis versus the Next statement
Basic syntax
' The following formula will return “abc”
dim x as number
dim MyArray() as string
MyArray = Array("a","b","c")
dim Concatenate as string
For x = 1 To count(MyArray) Step 1 ' Notice there is no Do
_ statement
Concatenate = Concatenate + MyArray(x)
Next x ' Notice the Next statement
Formula = Concatenate
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
The following example searches the Global array names for the name "Fred". If
it finds the name, it returns the index of the name in the array and exits out of
the For loop. Even though the For Counter variable is set to loop four times, the
formula only loops three times due to the Exit For statement.
Crystal syntax
stringvar array names;
names := ["Frank", "Helen", "Fred", "Linda"];
numbervar i;
numbervar output;
For i := 1 to 4 Do
( If names [i] = "Fred" Then
( output := i;
Exit For // Exit the loop once Fred is found
);
);
output
Basic syntax
Global names () As String
names = Array("Frank", "Helen", "Fred", "Linda")
Dim i
For i = 1 to 4
If names (i) = "Fred" Then
formula = i
Exit For ' Exit the loop once Fred is found
End If
Next I
Do Loops
• Do … While
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
• Do While … Loop
• Do Until … Loop
• Do … Loop While
• Do … Loop Until
Do … While
The Do ... While statement evaluates the expression once no matter what. It then
evaluates the condition, and if the condition is true, evaluates the expression
again. This process continues until the condition is false. This is the only Do
loop supported in Crystal syntax; it is not supported in Basic syntax.
' The following formula will return the number 0. The loop
_ will not evaluate.
dim x as number
Do While x = 2
x = x + 1
Loop
formula = x
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
' The following formula will return the number 6. The loop
_ will evaluate three times.
Dim x as number
Do Until x > 5
x = x + 2
Loop
formula = x
' The following formula will return the number 1. The loop
_ will evaluate once.
dim x as number
Do
x = x + 1
Loop While x = 2
formula = x
' The following formula will return the number 6. The loop
_ will evaluate six times.
dim x as number
Do
x = x + 1
Loop Until x > 5
formula = x
While Loops
While loops are similar to Do While Loops except that they generally do not
support Exit statements. While loops are supported in both Basic and Crystal
syntax . The main differences between Basic syntax and Crystal syntax are:
• Basic syntax ends a While loop with Wend whereas Crystal syntax encloses
the contents of the While loop with parenthesis
basic_vs_crystal_syntax.pdf
Crystal Reports Crystal versus Basic Formula syntax
Basic syntax
www.businessobjects.com
The Business Objects product and technology are protected by US patent numbers 5,555,403; 6,247,008;
6,578,027; 6,490,593; and 6,289,352. The Business Objects logo, the Business Objects tagline, BusinessObjects,
BusinessObjects Broadcast Agent, BusinessQuery, Crystal Analysis, Crystal Analysis Holos, Crystal Applications,
Crystal Enterprise, Crystal Info, Crystal Reports, Rapid Mart, and WebIntelligence are trademarks or registered
trademarks of Business Objects SA in the United States and/or other countries. Various product and service
names referenced herein may be trademarks of Business Objects SA. All other company, product, or brand
names mentioned herein, may be the trademarks of their respective owners. Specifications subject to change
without notice. Not responsible for errors or omissions. Copyright © 2004 Business Objects SA. All rights reserved.
basic_vs_crystal_syntax.pdf