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

If Statement Flow Chart
Figure 7.1: Decision Flow in VB6 If Statements

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.

IfStatement.vb
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

LoginSystem.vb
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
Login Form
Figure 7.2: Login Form
Successful Login
Figure 7.3: Successful Login

Example 2: Sales Commission Calculator

CommissionCalculator.vb
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
Commission Calculator
Figure 7.4: Commission Calculator

Example 3: Grade Calculator

GradeCalculator.vb
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.

IIfFunction.vb
Dim result = IIf(condition, valueIfTrue, valueIfFalse)

Example 4: Numeric and String Comparison

IIfExamples.vb
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
IIf Interface
Figure 7.5: IIf Test Interface
String Result
Figure 7.6: String Comparison
Numeric Result
Figure 7.7: Numeric Operation

Example 5: Login Status

LoginStatus.vb
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

Full VB6 Tutorial Index

Complete list of all VB6 lessons with descriptions

Explore Tutorials

Visual Basic Examples

Practical VB6 code samples for real-world applications

View Examples

VB6 Variables

Learn about variables in Visual Basic 6

Learn More

Select Case Statements

Master Select Case statements in VB6

Explore Next Lesson