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.

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.

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

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

Visual Basic 6 For Dummies 1st Edition

Create your own custom Visual Basic programs fast for Windows 95, Windows 98, and Windows NT!

If you want to develop business applications for Windows, then Microsoft’s Visual Basic 6 programming environment is for you. With its powerful and friendly object-oriented interface, Visual Basic 6 is the ideal tool for creating easy-to-use and attractive interfaces of your own. Whether you want to build original programs from the ground up or take advantage of existing wizards, Visual Basic 6 For Dummies will help you design and debug your work – and accomplish all your goals!

  • Covers using Visual Basic 6 with Windows 95, Windows 98, and Windows NT
  • Walks you through the ins and outs of object-oriented programming in Visual Basic and using the Visual Basic toolset
  • Shows you how to make use of menus and submenus, write subprograms that everyone can share, save and retrieve information in databases, integrate ActiveX controls into your applications, and build attractive user interfaces with buttons, boxes, and scroll bars

From the fundamentals of writing BASIC code to the thrill of distributing your very own custom applications, Visual Basic 6 For Dummies brings a fresh perspective to programming in Visual Basic 6 that everyone can understand.