Lesson 8: Mastering Select Case Statements
Learn how to simplify decision-making in VB6 with clean, efficient case structures
Key Takeaway
Select Case statements provide a cleaner, more efficient way to handle multiple conditions compared to nested If...Then...ElseIf statements. Master this structure to create more readable and maintainable VB6 applications.
Welcome to Lesson 8 of our Visual Basic 6 Tutorial! In this lesson, you'll master VB6's powerful Select Case structure that provides a cleaner alternative to complex If...Then...ElseIf statements when evaluating a single expression against multiple possible values.
8.1 Introduction to Select Case
The Select Case control structure is designed to simplify decision-making when you need to compare a single expression against multiple possible values. It's particularly useful for:
- Evaluating a variable against multiple constant values
- Replacing complex If-ElseIf chains
- Handling menu selections or command options
- Processing categorized data (grades, statuses, etc.)
Figure 8.1: Select Case Flow Chart
Case Value1, Case Value2, ... Case ValueN
If no match found, execute Case Else block
How Select Case Works
The Select Case statement evaluates an expression once and then compares it against multiple Case conditions. When it finds a matching Case, it executes the associated code block and then exits the Select Case structure. If no match is found, the Case Else block (if present) is executed. This structure is more efficient than multiple If...ElseIf statements when testing the same expression against multiple values.
Expression Evaluation
The expression is evaluated only once at the beginning of the Select Case statement.
Sequential Checking
Case conditions are checked in the order they appear in the code.
Single Execution
Only the first matching Case block is executed, even if multiple Cases match.
8.2 Select Case Syntax
The basic structure of the Select Case statement:
Select Case expression Case value1 ' Code to execute if expression = value1 Case value2 ' Code to execute if expression = value2 Case value3 ' Code to execute if expression = value3 Case Else ' Code to execute if no match is found End Select
Important Note
Always include a Case Else
block to handle unexpected values. This makes your code more robust and prevents unexpected behavior.
Example 1: Grade Evaluation
Private Sub Compute_Click() Dim grade As String grade = txtGrade.Text Select Case grade Case "A" result.Caption = "High Distinction" Case "A-" result.Caption = "Distinction" Case "B" result.Caption = "Credit" Case "C" result.Caption = "Pass" Case Else result.Caption = "Fail" End Select End Sub
Grade Evaluator:
8.3 Advanced Case Expressions
Select Case supports more complex expressions including ranges, multiple values, and comparisons:
Expression Type | Syntax | Example |
---|---|---|
Single Value | Case value |
Case "A" |
Multiple Values | Case value1, value2 |
Case "Mon", "Tue" |
Range | Case start To end |
Case 1 To 10 |
Comparison | Case Is operator value |
Case Is >= 50 |
Combination | Case value1, value2 To value3, Is operator value |
Case 1, 3 To 7, Is > 10 |
Example 2: Mark Evaluation with Ranges
Private Sub Compute_Click() Dim mark As Single mark = Val(txtMark.Text) Select Case mark Case Is >= 85 comment.Caption = "Excellence" Case 70 To 84 comment.Caption = "Good" Case 60 To 69 comment.Caption = "Above Average" Case 50 To 59 comment.Caption = "Average" Case Else comment.Caption = "Need to work harder" End Select End Sub
Figure 8.2: Mark Evaluation Form
Figure 8.3: Evaluation Result
Example 3: Number Guessing Game
Dim Secret_Number As Integer Private Sub Guess_Click() Dim Your_Number As Integer Your_Number = Val(InputBox("Enter a number between 1 and 6 inclusive")) Select Case Your_Number Case Is < Secret_Number MsgBox "Your number is smaller than the secret number, try again!" Case Is > Secret_Number MsgBox "Your number is greater than the secret number, try again!" Case Else Beep MsgBox "Your number is correct, congratulations!" End Select End Sub Private Sub Form_Load() Randomize Secret_Number = 1 + Int(6 * Rnd) End Sub
Example 4: Day Classifier NEW
Private Sub CheckDay_Click() Dim day As String day = txtDay.Text Select Case day Case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" result.Caption = "Weekday" Case "Saturday", "Sunday" result.Caption = "Weekend" Case Else result.Caption = "Invalid day" End Select End Sub
Example 5: Discount Calculator NEW
Private Sub Calculate_Click() Dim age As Integer Dim membership As String age = Val(txtAge.Text) membership = txtMembership.Text ' Using Select Case True for complex conditions Select Case True Case age < 18 discount.Caption = "0% (Under 18)" Case age >= 65 And membership = "Gold" discount.Caption = "25% (Senior Gold Member)" Case age >= 65 discount.Caption = "15% (Senior)" Case membership = "Gold" discount.Caption = "20% (Gold Member)" Case membership = "Silver" discount.Caption = "10% (Silver Member)" Case Else discount.Caption = "5% (Standard)" End Select End Sub
Advanced Technique
Using Select Case True
allows evaluating multiple Boolean expressions, providing great flexibility for complex conditional logic.
Lesson Summary
In this lesson, you've mastered the Select Case structure in VB6:
Cleaner Decision-Making
Select Case provides a more readable alternative to complex If-ElseIf chains.
Multiple Value Handling
Use commas to test multiple values in a single Case statement.
Range Evaluation
Use the To
keyword to test value ranges.
Comparison Operators
Use Is
with operators like >=, <, etc. for comparisons.
Case Else
Always include to handle unexpected values and make your code robust.
Best Practice
Use Select Case when evaluating a single expression against multiple values. For complex conditions with multiple variables, If statements might be more appropriate.
Practice Exercises
Test your understanding of Select Case with these exercises:
Exercise 1: Season Classifier
Create a program that takes a month number (1-12) and uses Select Case to display the corresponding season:
- December, January, February: Winter
- March, April, May: Spring
- June, July, August: Summer
- September, October, November: Fall
Exercise 2: Shipping Calculator
Create a shipping cost calculator based on package weight:
- 0-1 kg: $5.00
- 1.1-5 kg: $10.00
- 5.1-10 kg: $15.00
- Over 10 kg: $15 + $1.50 per additional kg
Use Select Case for the base rates and handle the over 10kg case separately.
Exercise 3: User Role Permissions
Implement a system that displays permissions based on user role codes:
- A: Full Administrative Access
- M: Manager Access (Create, Edit, Delete)
- E: Editor Access (Create, Edit)
- V: Viewer Access (Read Only)
- Default: No Access
Use a Select Case statement to evaluate the role code.
Next Lesson
Continue your VB6 journey with Lesson 9: Looping Structures.
Related Resources

Visual Basic 6 Made Easy
The ultimate beginner-friendly guide for mastering Windows-based application development using Visual Basic 6. Used as a textbook by universities worldwide.
What You'll Learn:
- Comprehensive coverage of VB6 coding techniques
- Event-driven programming best practices
- Practical examples and projects
- Debugging and error handling
- Database integration
- Advanced UI development

Visual Basic 2022 Made Easy
The ultimate guide to VB.NET programming in Visual Studio 2022. Master modern VB development with this comprehensive resource.
What You'll Learn:
- Modern VB.NET coding techniques
- Visual Studio 2022 features
- Advanced UI development
- Database programming with ADO.NET
- Web API integration
- Deployment strategies