Lesson 7: Mastering If Statements and IIf() Function
Learn decision-making in VB6 with conditional operators and logical expressions
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
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