Visual Basic 6.0 Guide
Select Case Statement
You have already used this structure in the Do My Maths exercise. It works in a very similar way to the If structure but is more useful when you have a lot of conditions to examine. The following code will message box the month which corresponds to a number input by the user.
Private Sub cmdWhatMonth_Click()
Dim intMonth As Integer
intMonth = txtMonth.Text
Select Case intMonth
Case 1
MsgBox "January"
Case 2
MsgBox "February"
Case 3
MsgBox "March"
{ This carries on up to 12 as you would expect }
Case Else
Msgbox "Enter a number from 1 -12 "
End Select
End Sub
Finish the code and create a working program.
The Case part of this structure works in a similar way to the If statement. It can be written differently though for different situations. Look at the examples below.
Case 0 To 15 | Tests whether the value is from 0 to 15 |
Case Is <40 | Tests whether the value is less than 40 |
Case 1, 3, 5 | Tests whether the value is 1, 3 or 5 |