VB6Parse / Library / Interaction / msgbox

VB6 Library Reference

MsgBox Function

Displays a message in a dialog box, waits for the user to click a button, and returns an Integer indicating which button the user clicked.

Syntax

MsgBox(prompt, [buttons], [title], [helpfile], [context])

Parameters

Return Value

Returns an Integer representing which button was clicked: - vbOK (1) - OK button was clicked - vbCancel (2) - Cancel button was clicked - vbAbort (3) - Abort button was clicked - vbRetry (4) - Retry button was clicked - vbIgnore (5) - Ignore button was clicked - vbYes (6) - Yes button was clicked - vbNo (7) - No button was clicked

Remarks

The MsgBox function is one of the most commonly used VB6 functions for user interaction and debugging. It provides a simple way to display messages, warnings, errors, and questions to the user.

Button Constants (First Group - Buttons):

Icon Constants (Second Group - Icons):

Default Button Constants (Third Group - Default Button):

Modality Constants (Fourth Group - Modality):

Other Constants (Fifth Group - Other):

Key Characteristics:

Common Use Cases:

Typical Uses

  1. Simple Messages - Display information to the user
  2. Error Handling - Show error messages with appropriate icons
  3. User Confirmation - Ask yes/no questions before proceeding
  4. Debugging - Display variable values during development
  5. Operation Feedback - Inform user of completion or status
  6. Validation Warnings - Alert user to invalid input
  7. Save Prompts - Ask user to save changes before closing
  8. Delete Confirmations - Verify user wants to delete items

Basic Examples

' Example 1: Simple message
MsgBox "Operation completed successfully!"
' Example 2: Message with title and icon
MsgBox "File not found!", vbExclamation, "Error"
' Example 3: Yes/No question
Dim result As Integer
result = MsgBox("Do you want to save changes?", vbYesNo + vbQuestion, "Confirm")
If result = vbYes Then
' Save changes
End If
' Example 4: Multi-line message
MsgBox "Line 1" & vbCrLf & "Line 2" & vbCrLf & "Line 3", vbInformation, "Multi-line"

Common Patterns

' Pattern 1: Simple error message
Sub ShowError(message As String)
MsgBox message, vbCritical, "Error"
End Sub
' Pattern 2: Confirmation with default No
Function ConfirmAction(message As String) As Boolean
Dim result As Integer
result = MsgBox(message, vbYesNo + vbQuestion + vbDefaultButton2, "Confirm")
ConfirmAction = (result = vbYes)
End Function
' Pattern 3: Save prompt
Function PromptToSave(fileName As String) As Integer
Dim msg As String
msg = "Save changes to " & fileName & "?"
PromptToSave = MsgBox(msg, vbYesNoCancel + vbQuestion, "Save Changes")
End Function
' Pattern 4: Display detailed error
Sub ShowDetailedError(errorMsg As String, errorNumber As Long)
Dim msg As String
msg = "Error #" & errorNumber & vbCrLf & vbCrLf & errorMsg
MsgBox msg, vbCritical + vbOKOnly, "Application Error"
End Sub
' Pattern 5: Information with sound
Sub ShowInfo(message As String)
MsgBox message, vbInformation + vbOKOnly, "Information"
' vbInformation plays the system information sound
End Sub
' Pattern 6: Retry/Cancel operation
Function RetryOperation(operation As String) As Boolean
Dim result As Integer
Dim msg As String

msg = "Failed to " & operation & "." & vbCrLf & "Would you like to retry?"
result = MsgBox(msg, vbRetryCancel + vbExclamation, "Operation Failed")
RetryOperation = (result = vbRetry)
End Function
' Pattern 7: Debug variable display
Sub DebugShow(variableName As String, value As Variant)
#If DEBUG_MODE Then
MsgBox variableName & " = " & CStr(value), vbInformation, "Debug"
#End If
End Sub
' Pattern 8: Abort/Retry/Ignore pattern
Function HandleError(errorMsg As String) As Integer
Dim msg As String
msg = "An error occurred:" & vbCrLf & vbCrLf & errorMsg & vbCrLf & vbCrLf & _
"Abort: Stop operation" & vbCrLf & _
"Retry: Try again" & vbCrLf & _
"Ignore: Continue anyway"
HandleError = MsgBox(msg, vbAbortRetryIgnore + vbCritical, "Error")
End Function
' Pattern 9: Formatted message with details
Sub ShowOperationResult(operation As String, recordCount As Long, elapsedTime As Double)
Dim msg As String
msg = "Operation: " & operation & vbCrLf & _
"Records processed: " & recordCount & vbCrLf & _
"Time elapsed: " & Format(elapsedTime, "0.00") & " seconds"
MsgBox msg, vbInformation, "Operation Complete"
End Sub
' Pattern 10: Custom button selection handler
Sub ProcessUserChoice(prompt As String)
Dim result As Integer
result = MsgBox(prompt, vbYesNoCancel + vbQuestion, "Choose Option")

Select Case result
Case vbYes
' Handle Yes
Case vbNo
' Handle No
Case vbCancel
' Handle Cancel
End Select
End Sub

Advanced Usage

Example 1: Smart Message Box Wrapper

' Module: SmartMessageBox
' Provides enhanced message box functionality with logging and customization

Option Explicit

Private m_logEnabled As Boolean
Private m_defaultTitle As String

Public Sub Initialize(appName As String, enableLogging As Boolean)
m_defaultTitle = appName
m_logEnabled = enableLogging
End Sub

Public Function ShowMessage(message As String, _
Optional msgType As VbMsgBoxStyle = vbInformation, _
Optional title As String = "") As Integer
Dim actualTitle As String

If Len(title) = 0 Then
actualTitle = m_defaultTitle
Else
actualTitle = title
End If

If m_logEnabled Then
LogMessage message, msgType
End If

ShowMessage = MsgBox(message, msgType, actualTitle)
End Function

Public Function Confirm(message As String, _
Optional defaultToNo As Boolean = False) As Boolean
Dim buttons As VbMsgBoxStyle
Dim result As Integer

buttons = vbYesNo + vbQuestion

If defaultToNo Then
buttons = buttons + vbDefaultButton2
End If

result = MsgBox(message, buttons, m_defaultTitle)
Confirm = (result = vbYes)
End Function

Public Sub ShowError(message As String, Optional errorNumber As Long = 0)
Dim msg As String

If errorNumber <> 0 Then
msg = "Error #" & errorNumber & vbCrLf & vbCrLf & message
Else
msg = message
End If

If m_logEnabled Then
LogMessage "ERROR: " & msg, vbCritical
End If

MsgBox msg, vbCritical, m_defaultTitle
End Sub

Public Sub ShowWarning(message As String)
If m_logEnabled Then
LogMessage "WARNING: " & message, vbExclamation
End If

MsgBox message, vbExclamation, m_defaultTitle
End Sub

Public Sub ShowInfo(message As String)
If m_logEnabled Then
LogMessage "INFO: " & message, vbInformation
End If

MsgBox message, vbInformation, m_defaultTitle
End Sub

Private Sub LogMessage(message As String, msgType As VbMsgBoxStyle)
' Log to file or debug window
Debug.Print Now & " - " & GetMessageTypeString(msgType) & ": " & message
End Sub

Private Function GetMessageTypeString(msgType As VbMsgBoxStyle) As String
If (msgType And vbCritical) = vbCritical Then
GetMessageTypeString = "ERROR"
ElseIf (msgType And vbExclamation) = vbExclamation Then
GetMessageTypeString = "WARNING"
ElseIf (msgType And vbQuestion) = vbQuestion Then
GetMessageTypeString = "QUESTION"
Else
GetMessageTypeString = "INFO"
End If
End Function

Example 2: Message Box Builder Class

' Class: MessageBoxBuilder
' Fluent interface for building complex message boxes

Option Explicit

Private m_prompt As String
Private m_title As String
Private m_buttons As VbMsgBoxStyle
Private m_icon As VbMsgBoxStyle
Private m_defaultButton As VbMsgBoxStyle

Private Sub Class_Initialize()
m_prompt = ""
m_title = ""
m_buttons = vbOKOnly
m_icon = 0
m_defaultButton = vbDefaultButton1
End Sub

Public Function WithPrompt(prompt As String) As MessageBoxBuilder
m_prompt = prompt
Set WithPrompt = Me
End Function

Public Function WithTitle(title As String) As MessageBoxBuilder
m_title = title
Set WithTitle = Me
End Function

Public Function WithButtons(buttons As VbMsgBoxStyle) As MessageBoxBuilder
m_buttons = buttons
Set WithButtons = Me
End Function

Public Function WithIcon(icon As VbMsgBoxStyle) As MessageBoxBuilder
m_icon = icon
Set WithIcon = Me
End Function

Public Function WithDefaultButton(defaultButton As VbMsgBoxStyle) As MessageBoxBuilder
m_defaultButton = defaultButton
Set WithDefaultButton = Me
End Function

Public Function AddLine(text As String) As MessageBoxBuilder
If Len(m_prompt) > 0 Then
m_prompt = m_prompt & vbCrLf
End If
m_prompt = m_prompt & text
Set AddLine = Me
End Function

Public Function Show() As Integer
Dim style As VbMsgBoxStyle
style = m_buttons Or m_icon Or m_defaultButton
Show = MsgBox(m_prompt, style, m_title)
End Function

' Convenience methods
Public Function ShowError(errorMsg As String) As Integer
Set WithPrompt(errorMsg)
Set WithIcon(vbCritical)
ShowError = Show()
End Function

Public Function AskYesNo(question As String) As Boolean
Set WithPrompt(question)
Set WithButtons(vbYesNo)
Set WithIcon(vbQuestion)
AskYesNo = (Show() = vbYes)
End Function

Example 3: Message Queue Manager

' Class: MessageQueueManager
' Manages queued messages to avoid overwhelming the user

Option Explicit

Private Type QueuedMessage
prompt As String
buttons As VbMsgBoxStyle
title As String
timestamp As Date
End Type

Private m_queue As Collection
Private m_maxQueueSize As Long
Private m_autoShowDelay As Long ' milliseconds

Private Sub Class_Initialize()
Set m_queue = New Collection
m_maxQueueSize = 10
m_autoShowDelay = 1000
End Sub

Public Sub QueueMessage(prompt As String, _
Optional buttons As VbMsgBoxStyle = vbOKOnly, _
Optional title As String = "Message")
Dim msg As QueuedMessage

msg.prompt = prompt
msg.buttons = buttons
msg.title = title
msg.timestamp = Now

If m_queue.Count >= m_maxQueueSize Then
' Remove oldest message
m_queue.Remove 1
End If

m_queue.Add msg
End Sub

Public Sub ShowNextMessage() As Integer
Dim msg As QueuedMessage

If m_queue.Count > 0 Then
msg = m_queue(1)
m_queue.Remove 1
ShowNextMessage = MsgBox(msg.prompt, msg.buttons, msg.title)
End If
End Sub

Public Sub ShowAllMessages()
Dim msg As QueuedMessage
Dim i As Long

For i = 1 To m_queue.Count
msg = m_queue(i)
MsgBox msg.prompt, msg.buttons, msg.title
Next i

Set m_queue = New Collection
End Sub

Public Function GetQueuedCount() As Long
GetQueuedCount = m_queue.Count
End Function

Public Sub ShowSummary()
Dim msg As String
Dim qMsg As QueuedMessage
Dim i As Long

If m_queue.Count = 0 Then
MsgBox "No queued messages", vbInformation
Exit Sub
End If

msg = "Queued Messages (" & m_queue.Count & "):" & vbCrLf & vbCrLf

For i = 1 To m_queue.Count
qMsg = m_queue(i)
msg = msg & i & ". " & Left(qMsg.prompt, 50)
If Len(qMsg.prompt) > 50 Then msg = msg & "..."
msg = msg & vbCrLf
Next i

MsgBox msg, vbInformation, "Message Queue"
End Sub

Example 4: Auto-Dismissing Message Box (Timer-based)

' Module: TimedMessageBox
' Shows message boxes that auto-dismiss after timeout

Option Explicit

#If Win32 Then
Private Declare Function MessageBoxTimeout Lib "user32" Alias "MessageBoxTimeoutA" ( _
ByVal hwnd As Long, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal uType As Long, _
ByVal wLanguageId As Long, _
ByVal dwMilliseconds As Long) As Long
#End If

Public Function MsgBoxTimed(prompt As String, _
Optional buttons As VbMsgBoxStyle = vbOKOnly, _
Optional title As String = "", _
Optional timeout As Long = 5000) As Integer
#If Win32 Then
' Use Windows API for timed message box
MsgBoxTimed = MessageBoxTimeout(0, prompt, title, buttons, 0, timeout)
#Else
' Fallback to regular MsgBox
MsgBoxTimed = MsgBox(prompt, buttons, title)
#End If
End Function

Public Sub ShowTimedInfo(message As String, Optional seconds As Long = 3)
MsgBoxTimed message, vbInformation, "Information", seconds * 1000
End Sub

Public Sub ShowTimedWarning(message As String, Optional seconds As Long = 5)
MsgBoxTimed message, vbExclamation, "Warning", seconds * 1000
End Sub

Public Function ConfirmTimed(message As String, Optional seconds As Long = 10) As Boolean
Dim result As Integer
result = MsgBoxTimed(message, vbYesNo + vbQuestion, "Confirm", seconds * 1000)
ConfirmTimed = (result = vbYes)
End Function

Error Handling

' MsgBox rarely fails, but handle potential issues:
On Error Resume Next
result = MsgBox(userInput, vbOKOnly, "Message")
If Err.Number <> 0 Then
Debug.Print "MsgBox error: " & Err.Description
' Possibly userInput was too long or contained invalid characters
End If
On Error GoTo 0

Performance Considerations

Best Practices

  1. Keep messages concise - Users don't read long messages
  2. Use appropriate icons - Help users understand message severity
  3. Provide clear actions - Button choices should be obvious
  4. Use meaningful titles - Don't just say "Error" or "Message"
  5. Avoid MsgBox in loops - Queue messages or use alternative feedback
  6. Handle all return values - Check what button user clicked
  7. Use vbCrLf for readability - Multi-line messages are easier to read
  8. Consider default button - Make safe choice the default
  9. Test message length - Very long messages may not display well
  10. Use for errors, not debugging - Prefer Debug.Print for development

Comparison with Alternatives

Approach Pros Cons
MsgBox Simple, built-in, modal Blocks execution, limited customization
Custom Form Full control, rich UI More code, more complex
Debug.Print Non-blocking, fast Not visible to end users
Status Bar Non-blocking, professional Limited message length, less visible
InputBox Gets user input Only single-line text input
Notification API Modern, non-blocking Requires Windows 10+, more complex

Statement vs Function

MsgBox can be used as both a statement (no parentheses, no return value) and a function (with parentheses, returns value):

' As a statement (no return value needed)
MsgBox "Hello, World!"

' As a function (capture return value)
result = MsgBox("Continue?", vbYesNo)

Platform Notes

Limitations

VB6 Parser Notes

MsgBox can be used as both a statement and a function. When used as a function (with parentheses and capturing return value), it is parsed as a CallExpression. When used as a statement (without parentheses), it may be parsed differently. This module exists primarily for documentation purposes to provide comprehensive reference material for VB6 developers working with user interaction and message display operations.

← Back to Interaction | View all functions