VB 6 – Switch Function

The Switch() function works similar to If-Then-Else construct, but in a different way. The function checks few conditions and if one of the condition is passed, then the statement corresponding to that condition is evaluated or displayed.

Advertisements

Syntax: Switch Function

The syntax of the Switch() function is given below.

Variable = Switch(condition 1, Value 1, condition 2 , Value 2,..., condition n, Value n)

The switch statement goes through each condition and whichever evaluate to true, the value part is returned and assigned to a variable on left hand side.

Advertisements

Example Program: Switch Function

In this example, we are going to accept an input number, then check the number if it is greater than or equal to 100 or less than 100 and display the result.

Private Sub Command1_Click()
Dim number As Integer, Result As String
number = InputBox("Enter a Number")
Result = Switch(number < 100, "Number is Less than 100", number >= 100, "Number is Greater than Equal to 100")
MsgBox (Result)
End Sub

Input to Switch() Function

Input-Switch-Function
Figure 1 – Input-Switch-Function

When the user enters a number, the program checks the number and display the result as follows.

Output – Switch() Function

Output - Switch -Function
Figure 2 – Output – Switch -Function

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.