CitectVBA Programming Reference > CitectVBA Function Reference > Conditional Statements > Select

Select

The Select Case statement tests the same variable for many different conditions. The test value provided with the initial Select Case statement is logically tested against the Case test condition.

The Select Case structure can perform different blocks of statements dependant upon whichever Case statement test condition (if more than one) first results as True, through the use of the Case statement block:

Select Case <TestValue>
Case <Condition>
' Case statement block
' perform only if case true
<Statement/s>
Case Else
' Else statement block
' perform only if all cases false
<Statement/s>
End Select

If the result of the Case test condition was True, the program flow performs the statements contained within that Case statement block, and will then exit the Select Case structure (without performing any of the Else statement block statements).

If the result of the Case test condition was False, the program flow jumps over the Case statement block (without performing any of those statements) to the Case Else statement to perform the statements in the Else statement block until it reaches the End Select statement.

Further test conditions can be placed into a Select Case structure through the optional use of further Case statement blocks. Case statement blocks can only be positioned within a Select Case structure before the Case Else statement block.

Select Case <TestValue>
Case <Condition>
' Case statement block
' perform only if case true
<Statement/s>
Case <Condition>
' Case statement block
' perform only if case true
<Statement/s>
Case Else
' Else statement block
' perform only if all cases false
<Statement/s>
End Select

Each Case statement block is evaluated in order until the test condition of one results as True. The program flow performs the statements contained within that Case statement block, and will then exit the Select Case structure (without performing any other statements).

The statements of ONLY one Case statement block are ever performed, unless all result in False and there is no Case Else block declared, in which case no Case statement blocks are performed at all.

The following example may help clarify the logic testing being performed in a Select Case structure. Lets say that we have a variable named (intDayOfWeek) containing an integer (ranging from 1 to 7) representing the day of the week, and we wished to display that value as a string (named strDayOfWeek) containing the name of the day of the week, assuming in this example, that Sunday is the first day of the week (1). The Select Case structure would look like this:

Dim strDayOfWeek As String

Select Case intDayOfWeek
Case = 1
StrDayOfWeek = "Sunday"
Case = 2
StrDayOfWeek = "Monday"
Case = 3
StrDayOfWeek = "Tuesday"
Case = 4
StrDayOfWeek = "Wednesday"
Case = 5
StrDayOfWeek = "Thursday"
Case = 6
StrDayOfWeek = "Friday"
Case = 7
StrDayOfWeek = "Saturday"
Case Else
StrDayOfWeek = "Invalid"
End Select

The Select Case structure tends to be easier to read, understand, and follow and should be used in place of a complicated multi-nested If...ElseIf structure.