Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 118,795 times

Contents

Related Categories

Select Case statement - Usage

Usage

Sub Command1_Click()
   Select Case Text1.Text
      Case "Hello"
         Msgbox "Welcome!"
      Case "Goodbye"
         Msgbox "See you again soon!"
      Case Else
         Msgbox "You said something else"
   End Select
End Sub

This retrieves the value of Text1.Text and then tests it against each case. If Text1.Text is equal to "Hello" then a message box is displayed saying "Welcome".  If Text1.Text is equal to "Goodbye" then a message box is displayed saying "See you again soon". If Text1.Text is equal to something else then a message box is displayed saying "You said something else".

' Initialise variable
Dim Number As Integer
Number = 8
Select Case Number    ' Evaluate Number.
Case 1 To 5    ' Number between 1 and 5.
    Msgbox "Between 1 and 5"
Case 6, 7, 8    ' Number between 6 and 8.
    Msgbox "Between 6 and 8"
Case Is > 8 And Number < 11    ' Number is 9 or 10.
    Msgbox "Greater than 8"
Case Else    ' Other values.
    Msgbox "Not between 1 and 10"
End Select

This code evaluates the variable Number, and then runs the appropriate code. Note you need to use the statement Is if you are using comparison operators (i.e. = ,<=, > )

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments