Making a choice with the Select Case statement

The block statement Select Case specifies conditional execution of one group of statements selected from one or more groups, depending on the value of an expression. It is similar to the If...Then...ElseIf statement.

The syntax is:

Select Case selectExpr

[ Case conditionList

[ statements ] ]

[ Case conditionList

[ statements ] ]

...

[ Case Else

[ statements ] ]

End Select

At run time, the Select Case statement compares the value of a single selectExpr expression with the values established by each conditionList. It executes the statements for the first conditionList matched by the value of selectExpr. Either a single group of statementsis executed, or none is executed. If you include a Case??Else clause, it's executed only if selectExpr fails all conditions in all condition lists. After a clause is executed, LotusScript® continues execution at the first statement following the End Select statement.

This example adds a suffix to a whole number to turn it into an ordinal number. The script defines and calls the function SetOrd, which accepts a string argument, determines whether it is of the correct kind, and returns either a message about the argument or a string showing the argument with the correct suffix.

Function SetOrd (anInt As String) As String 
   Dim printNum As String
   ' If argument can't be converted to a number,
   ' assign a message and do nothing more.
   If Not IsNumeric(anInt$) Then
      SetOrd$ = "That's not a number."
      Exit Function
   ' If argument is not a whole number,
   ' assign a message and do nothing more.
   ElseIf Fraction(CSng(anInt$)) <> 0 Then
      SetOrd$ = "That's not a whole number."
      Exit Function
   ' If number is not in range, assign a message
   ' and do nothing more.
   ElseIf CInt(anInt$) > 50 Or CInt(anInt$) < 0 Then
      SetOrd$ = "That number's out of range."
      Exit Function
   End If
   ' Determine and append the correct suffix. 
   Select Case anInt$
      Case "1", "21", "31", "41": printNum$ = anInt$ & "st"
      Case "2", "22", "32", "42": printNum$ = anInt$ & "nd"
      Case "3", "23", "33", "43": printNum$ = anInt$ & "rd"
      Case Else: printNum$ = anInt$ & "th"
   End Select
   SetOrd$ = "This is the " & printNum$ & " number."
End Function
' Call the function.
MessageBox(SetOrd(InputBox$("Enter a whole number between" & _
   " 0 and 50:")))

The last line of the example is the only executable code outside of the function SetOrd and instructs the MessageBox statement to display a message based on the user input received by the InputBox$ function. The value entered by the user is passed to SetOrd, which determines what MessageBox displays.