Mastering If Statements in VB6
Learn decision-making in VB6 with conditional operators and logical expressions
Lesson Overview
Key Takeaway
Decision-making is a fundamental programming skill that enables your applications to respond intelligently to different conditions. Master If statements and IIf() function to create dynamic applications.
Welcome to Lesson 7 of our Visual Basic 6 Tutorial! In this lesson, you'll master VB6's powerful conditional operators and logical expressions that allow your programs to make decisions based on user input, data values, and system states.
7.1 Conditional Operators
Conditional operators allow your VB6 programs to compare values and determine program flow. These operators form the foundation of decision-making structures in programming:
| Operator | Meaning | Example |
|---|---|---|
= |
Equal to | If x = y Then |
> |
Greater than | If x > y Then |
< |
Less than | If x < y Then |
>= |
Greater than or equal to | If x >= y Then |
<= |
Less than or equal to | If x <= y Then |
<> |
Not equal to | If x <> y Then |
Important Note
When comparing strings, uppercase letters are considered "less than" lowercase letters, and numbers are "less than" letters.
7.2 Logical Operators
Logical operators allow you to combine multiple conditions to create complex decision structures:
| Operator | Description | Example |
|---|---|---|
And |
Both conditions must be true | If x > 5 And x < 10 Then |
Or |
At least one condition must be true | If x < 0 Or x > 100 Then |
Xor |
Exactly one condition must be true | If x > 0 Xor y > 0 Then |
Not |
Reverses the logical value | If Not x = y Then |
If Statement Flow
7.3 Using If...Then...Else Statements
The If...Then...Else structure is the cornerstone of decision-making in VB6. It allows your program to execute different code blocks based on specified conditions.
If condition Then ' Code to execute if condition is true ElseIf anotherCondition Then ' Code to execute if another condition is true Else ' Code to execute if no conditions are true End If
Example 1: Login System
Private Sub OK_Click() Dim username, password As String username = "John123" password = "qwertyupi#@" If UsrTxt.Text = username And pwTxt.Text = password Then MsgBox ("Sign in successful") ElseIf UsrTxt.Text <> username Or pwTxt.Text <> password Then MsgBox ("Sign in failed") End If End Sub
Example 2: Sales Commission Calculator
Private Sub cmdCalComm_Click() Dim salevol, comm As Currency salevol = Val(TxtSaleVol.Text) If salevol >= 5000 And salevol < 10000 Then comm = salevol * 0.05 ElseIf salevol >= 10000 And salevol < 15000 Then comm = salevol * 0.1 ElseIf salevol >= 15000 And salevol < 20000 Then comm = salevol * 0.15 ElseIf salevol >= 20000 Then comm = salevol * 0.2 Else comm = 0 End If LblComm.Caption = Format(comm, "$#,##0.00") End Sub
Example 3: Grade Calculator
Private Sub CalculateGrade_Click() Dim score As Integer Dim grade As String score = Val(txtScore.Text) If score >= 90 Then grade = "A" ElseIf score >= 80 Then grade = "B" ElseIf score >= 70 Then grade = "C" ElseIf score >= 60 Then grade = "D" Else grade = "F" End If lblGrade.Caption = "Grade: " & grade End Sub
7.4 The IIf() Function
The IIf (Immediate If) function provides a concise way to make simple decisions. It evaluates a condition and returns one of two values based on whether the condition is true or false.
Dim result = IIf(condition, valueIfTrue, valueIfFalse)
Example 4: Numeric and String Comparison
Private Sub CmdNumeric_Click() Dim x, y, a, b, ans As Double x = InputBox("Enter a number") y = InputBox("Enter another number") a = Val(x) b = Val(y) ans = IIf(a < b, b - a, a * b) MsgBox ans End Sub Private Sub CmdString_Click() Dim A, B, C As String A = InputBox("Enter a word") B = InputBox("Enter another word") C = IIf(A < B, A, B) MsgBox C End Sub
Example 5: Login Status
Private Sub ShowStatus_Click() Dim isLoggedIn As Boolean Dim statusMessage As String ' Check login status (in real app, this would come from authentication) isLoggedIn = (txtUsername.Text = "admin" And txtPassword.Text = "secret") ' Use IIf to determine status message statusMessage = IIf(isLoggedIn, _ "Welcome " & txtUsername.Text & "! You are logged in.", _ "Login failed. Please check your credentials.") lblStatus.Caption = statusMessage End Sub
Important Note
IIf() is ideal for simple, single-condition decisions. For more complex logic with multiple conditions, use If...Then...Else statements.
Lesson Summary
In this lesson, you've mastered essential VB6 decision-making techniques:
Conditional Operators
Use =, >, <, >=, <=, and <> to compare values and make decisions based on comparisons.
Logical Operators
Combine conditions with And, Or, Xor, and Not to create complex decision logic.
If...Then...Else Statements
The primary structure for controlling program flow based on conditions. Supports multiple ElseIf blocks and a final Else block.
IIf() Function
A compact function for simple conditional evaluations that returns one of two values based on a condition.
Best Practice
Use If...Then...Else for complex multi-condition logic and IIf() for simple, single-condition decisions where you need to return a value.
Next Lesson
Continue your VB6 journey with Lesson 8: Select Case Statements.
Related Resources
Visual Basic 6 Made Easy
Start your programming journey with Visual Basic 6. Learn how to build Windows applications step-by-step using an easy and beginner-friendly approach.
- Perfect for beginners
- Learn core programming concepts
- Build real VB6 applications
Visual Basic 2026 Made Easy
Upgrade to modern Visual Basic with VB.NET, .NET 10, and Visual Studio 2026. Build real-world applications with modern tools and AI-powered development.
- Modern VB.NET development
- Hands-on projects and real apps
- GitHub Copilot & AI integration
๐ Ready for the Next Level?
After learning the basics with VB6 Made Easy and upgrading to VB2026 Made Easy, continue your journey into advanced professional development.
Advanced VB.NET Programming
Take your VB.NET skills to the next level. Learn advanced programming techniques, real-world architectures, and professional development practices using modern .NET.
Best For:
- After VB6 or VB.NET fundamentals
- Intermediate to advanced learners
- Developers building real-world systems
๐ Migrating from VB6 to VB.NET
Still using or learning classic Visual Basic 6? This practical step-by-step guide shows you how to migrate legacy VB6 applications to modern VB.NET with Visual Studio 2026 and .NET 10.
VB6 to Modern VB.NET Made Easy
A practical step-by-step guide to migrating legacy Visual Basic 6 applications to VB.NET with Visual Studio 2026 and .NET 10.
- Designed for VB6 programmers and legacy app maintainers
- Clear migration guidance from classic VB to modern .NET
- Perfect bridge between VB6 fundamentals and VB.NET development
๐ Move to Modern VB.NET
Visual Basic 6 is your foundation โ but modern development uses VB.NET with .NET and Visual Studio 2026.
Start VB.NET Tutorial โ