Lesson 10 ยท Built-in Functions

Mastering VB6 Functions

Learn how to use MsgBox and InputBox for effective user interaction

Lesson reading progress

Lesson Overview

Lesson10 of 40
TopicBuilt-in Functions
FocusMsgBox ยท InputBox
GoalUse essential user prompts
Upgrade PathVB.NET / Visual Studio 2026

Key Takeaway

MsgBox and InputBox are essential built-in functions in VB6 for creating interactive applications. They allow you to communicate with users, gather input, and make decisions based on user responses.

Welcome to Lesson 10 of our Visual Basic 6 Tutorial! In this lesson, you'll master VB6's built-in functions for user interaction. These functions are crucial for creating applications that communicate with users through dialog boxes.

10.1 The MsgBox Function

The MsgBox function displays a dialog box with a message and waits for the user to click a button. Its syntax is:

MsgBox Syntax
response = MsgBox(prompt[, buttons] [, title])

Button Styles

Value Constant Buttons
0 vbOKOnly OK only
1 vbOKCancel OK and Cancel
2 vbAbortRetryIgnore Abort, Retry, Ignore
3 vbYesNoCancel Yes, No, Cancel
4 vbYesNo Yes and No
5 vbRetryCancel Retry and Cancel

Icon Styles

Value Constant Icon
16 vbCritical Critical
32 vbQuestion Question
48 vbExclamation Exclamation
64 vbInformation Information

Return Values

Value Constant Button
1 vbOK OK
2 vbCancel Cancel
3 vbAbort Abort
4 vbRetry Retry
5 vbIgnore Ignore
6 vbYes Yes
7 vbNo No

Example 10.1: Basic MsgBox Usage

Confirmation.vb
Private Sub Test_Click()
    Dim response As Integer
    
    response = MsgBox("Do you want to continue processing?", vbYesNo + vbQuestion, "Confirmation")
    
    If response = vbYes Then
        lblStatus.Caption = "Processing continued"
    Else
        lblStatus.Caption = "Processing canceled"
    End If
End Sub

MsgBox Simulation:

Do you want to continue processing?

Yes
No

Example 10.2: MsgBox with Multiple Buttons

Private Sub Test2_Click()  
    Dim testMsg2 As Integer 
    testMsg2 = MsgBox("Click to Test", vbYesNoCancel + vbExclamation, "TestMessage")
    
    If testMsg2 = vbYes Then 
        display2.Caption = "Testing successful"
    ElseIf testMsg2 = vbNo Then 
        display2.Caption = "Are you sure?"
    Else 
        display2.Caption = "Testing fail"
    End If
End Sub 
MsgBox with Question Icon

Figure 10.1: MsgBox with Question Icon

MsgBox with Multiple Buttons

Figure 10.2: MsgBox with Yes/No/Cancel

10.2 The InputBox Function

The InputBox function displays a dialog with a prompt where users can enter text. Its syntax is:

InputBox Syntax
userInput = InputBox(prompt[, title] [, default] [, xpos] [, ypos])

Important Note

Always validate InputBox responses. Users might cancel the dialog (returning empty string) or enter invalid data. Use IsNumeric() for numbers and handle empty strings appropriately.

Example 10.3: Basic InputBox Usage

Greeting.vb
Private Sub cmdGreet_Click()
    Dim userName As String
    
    userName = InputBox("Please enter your name:", "User Identification", "John Doe")
    
    If userName <> "" Then
        lblGreeting.Caption = "Hello, " & userName & "! Welcome to our application."
    Else
        lblGreeting.Caption = "No name provided. Welcome, guest!"
    End If
End Sub

InputBox Simulation:

Please enter your name:

OK
Cancel

Example 10.4: User Configuration with InputBox

Private Sub cmdConfigure_Click()
    Dim fontSize As String
    Dim fontColor As String
    
    ' Get font size preference
    fontSize = InputBox("Enter preferred font size (8-24):", "Display Settings", "12")
    
    ' Validate input
    If IsNumeric(fontSize) Then
        If Val(fontSize) >= 8 And Val(fontSize) <= 24 Then
            txtDisplay.FontSize = Val(fontSize)
        Else
            MsgBox "Font size must be between 8 and 24. Using default.", vbExclamation
        End If
    Else
        MsgBox "Invalid number format. Using default size.", vbExclamation
    End If
    
    ' Get color preference
    fontColor = InputBox("Enter font color (red, blue, green):", "Display Settings", "black")
    
    Select Case LCase(fontColor)
        Case "red": txtDisplay.ForeColor = vbRed
        Case "blue": txtDisplay.ForeColor = vbBlue
        Case "green": txtDisplay.ForeColor = vbGreen
        Case Else: MsgBox "Color not recognized. Using default.", vbInformation
    End Select
End Sub

Explanation: This example demonstrates using InputBox for multiple configuration settings with validation. It shows how to handle different data types and provide user feedback.

Lesson Summary

In this lesson, you've mastered VB6's essential built-in functions for user interaction:

MsgBox Function

Displays messages, warnings, and prompts users for decisions

InputBox Function

Collects text input from users through simple dialogs

Button Styles

Customize dialog buttons with constants like vbYesNo, vbOKCancel

Icon Options

Enhance dialogs with icons like vbQuestion, vbExclamation

Validation

Always validate user input from InputBox for correctness

Best Practice

Use MsgBox for decisions and alerts, and InputBox for simple text input. For complex data collection, create custom forms instead of multiple InputBox dialogs.

Practice Exercises

Test your understanding of MsgBox and InputBox with these exercises:

Exercise 1: Age Verification

Create a program that asks for the user's age using InputBox. If the age is below 18, show a MsgBox saying "Access denied - you must be 18 or older". If 18 or older, show "Access granted".

Exercise 2: Simple Calculator

Create a calculator that uses InputBox to get two numbers from the user. Then use MsgBox with vbYesNoCancel to ask which operation to perform (Add, Subtract, Multiply). Display the result in another MsgBox.

Exercise 3: Password Strength Checker

Use InputBox to get a password from the user. Check if it's at least 8 characters long and contains both letters and numbers. Use MsgBox to display whether the password is strong or weak.

Exercise 4: Feedback System

Create a feedback form with InputBox to collect user comments. Then show a confirmation MsgBox asking "Submit feedback?" with Yes/No options. If Yes, display a thank you message.

Exercise 5: Custom Dialog

Create a program that asks for a filename using InputBox. Validate that it ends with ".txt". If not, show a MsgBox asking if the user wants to automatically add the extension.

Next Lesson

Continue your VB6 journey with Lesson 11: Math Functions.

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 Looping Structures

Learn about looping in Visual Basic 6

Previous Lesson

Math Functions

Master math functions in VB6

Next Lesson

๐Ÿš€ 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 โ†’