Mastering VB6 Functions
Learn how to use MsgBox and InputBox for effective user interaction
Lesson Overview
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:
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
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?
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
Figure 10.1: MsgBox with Question Icon
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:
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
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:
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
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 โ