Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

Comparing Crystal Syntax and Basic Syntax

Crystal Reports uses the Formula Workshop to perform dynamic formatting of the objects and sections in a report. The Formula Workshop uses one of two programming languages to do this: Crystal syntax and Basic syntax. While these languages are functionally equivalent, their syntax is very different.

Crystal syntax is the original syntax of Crystal Reports. It is very similar the the C language. I have a hunch that the first version of Crystal Reports was probably written in C or C++, and consequently the developers were comfortable creating a language very similar to that for Crystal Reports.

Basic syntax was created b/c many people using Crystal Reports are already familiar with the BASIC language via college classes or using Visual Basic for Applications (VBA) in the MS Office products. That similarity makes it easy for people to quickly grasp its concepts.

In this discussion we’ll compare the two syntaxes and highlight their primary differences.

When writing code, it is very important to write comments that explain what the code is doing. I know from experience that no matter how much the code makes sense at the moment I’m writing it, when I look at it six months later I often think, “What was I doing here?”. Adding comments helps avoid confusion in the future.

Crystal syntax uses a double-slash to designate a comment. They can appear at the beginning of a line or after the code. Anything appearing after the slashes is ignored.
//This is a comment in Crystal syntax
X := X+5; //this last part is a comment

Basic syntax uses an apostrophe for comments.
‘This is a comment in Basic syntax

A code statement is typically fairly short and comprised of one line of typing. But sometimes the code can get complex and require more than one line to complete. Thus, there needs to be a way to terminate a line; and each language handles it differently. Crystal syntax uses a semi-colon to mark the end of a line. Even if the code only uses a single line, it still needs to be terminated with a semi-colon. Here are two examples:
Age := 22;
PersonName := {people.first_name} + ‘ ‘ +
{people.last_name};

Basic syntax assumes that most code can fit on a single line. So there is no line terminator (well, the hidden ‘carriage return’ character is the terminator). But if you need to use two lines, then mark it with the line continuation character (an underscore).
PersonName = {people.first_name} & ” ” & _
{people.last_name}

In Crystal Reports, formulas are often used to return a value that is printed on the report. For example, in the code section above, if this code were used in a report formula then the PersonName variable is returned to the report for printing. In Crystal syntax, the last value referenced in a formula is the value returned. You can ‘reference’ a value by assigning it to a variable or just putting it on a line by itself. Here are two examples.
X := 5; //the value 5 will be returned by the formula

Age := 23;
Age; //The value of the variable Age, currently 23, is returned by the formula

Basic syntax makes it more clear as to what is being returned. You have to specifically assign a value to the ‘Formula’ variable name to return the value. Here is an example:

Age = 23
Formula := Age

This discussion only highlighted some of the fundamental syntax differences between the two languages. If you want me to go into more depth about the syntax differences for looping structures, data types and function names, just give me a ‘thumbs up’ or post a question below.