Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

8.08 If Then, Select Case

Conditional Structures

Conditional structures provide you with a way of testing one or more variables to see if they are equal to a value or are within a range of values. If the test succeeds, then a code block is executed. If the test fails, then a different code block is executed. Since there are many different circumstances where you will want to do this, Basic syntax provides you with a lot of options to match your circumstance. Each has its own benefits and drawbacks that you need to consider when deciding which to use. The conditional structures are: If, and Select Case.

The If statement uses the standard VB .NET syntax of testing a condition and performing one action if it’s true and another action if it’s false. The code in the Else block is executed if the test returns false. Basic syntax also supports the Else If statement. Finish an If block with End If.

If condition1 Then
…code...
ElseIf condition2 Then
...code...
Else
...code...
End If

Crystal syntax does not have an End If statement. It also considers the entire If block a single statement. Put the line terminator after the Else block to terminate it. The Else If keyword is actually two words. If there is more than one statement within a code block then enclose the statements within parentheses and use the semicolon to terminate each statement.

If condition1 Then
…code...
Else If condition2 Then
(
...code...;
...code...;
)
Else
...code;

The Select Case statement uses the standard VB .NET syntax of putting the variable to be tested at the end of the Select Case statement. After the Select Case statement, list the test conditions and the related code blocks using the Case statement. You can list multiple conditions for a single Case statement by separating the conditions with a comma. If none of the Case statements return True, then the code in the Case Else block is executed. Finish a Select Case block with End Select.

Select Case var
Case condition1
...code...
Case condition1, condition2
...code...
Case Else
...code...
End Select

Crystal syntax Select statements are slightly different than Basic syntax. The Case keyword is only used for testing the conditions. Put a colon at the end of the condition. If no conditions match the value being tested, the program executes the Default case. There is no End keyword.

Select var
Case condition1:
...code...
Case condition1, condition2:
...code...
Default:
...code...