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.

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.
Buy at

This is an excerpt from the book Crystal Reports Encyclopedia. Click to read more chapter excerpts.

Crystal Report Looping Structures

Looping structures let you execute a block of code multiple times. The number of times this code block is executed depends upon the type of loop used and what happens within the code block. The looping structures covered are: For Next, While, and a variety of Do loops.

For Next Loop

The For Next loop has the syntax of using the For statement followed by a variable and the start and ending range. You have to decide in advance how many times the code block gets executed.

The default loop increment is 1. Use the Step keyword to define a new increment. Terminate the For block using the Next statement. Putting the variable name after the Next statement is optional. You can prematurely exit the loop by using the Exit For statement.

For var = start To end Step increment
         ...code...
         If condition Then
                   Exit For
         End If
Next

Crystal syntax uses := to assign the loop range and it has the Do keyword at the end of the line. Rather than terminating the loop with the Next keyword, it requires parentheses to surround the code block.

For var := start To end Step increment Do
(
         ...code...
         If condition Then
                   Exit For
         End If
)

While and Do Loops

The While and Do loops all have the standard syntax. The While keyword is used to continue looping as long as the condition evaluates to True. The While block is terminated with a Wend statement. The Do loops are terminated with a Loop statement. The Until keyword is used to continue looping when a condition evaluates to False. You can exit a Do loop with an Exit Do statement.

Code template for While … Wend:

While true_condition
         ...code...
Wend

Code template for Do While … Loop:

Do While true_condition
         ...code...
Loop

Code template for Do Until … Loop:

Do Until false_condition
         ...code...
Loop

Code template for Do … Loop While:

Do
         ..code...
Loop While true_condition

Code template for Do … Loop Until:

Do
         ..code...
Loop Until false_condition

Crystal syntax has few looping structures to choose from. It has the While...Do loop and the Do...While loop. It uses parentheses to define the code block.

Code template for While … Do:

While true_condition Do
(
         ...code...
)

Code template for Do...While:

Do
(
         ...code...
) While true_condition


To read all my books online, click here for the Crystal Reports ebooks.