MsgBox - VBScript


Overview

MsgBox command in VB Script displays a message box with a specified text, title, and buttons. It is commonly used for user interaction, error handling, and providing information.

Syntax

MsgBox(Msg, Options, Title)

Options/Flags

  • OKOnly (Default): Displays only an OK button.
  • MsgBoxStyle_Critical (16): Displays a critical error icon.
  • MsgBoxStyle_Exclamation (48): Displays an exclamation icon.
  • MsgBoxStyle_Information (64): Displays an information icon.
  • MsgBoxStyle_Question (32): Displays a question mark icon.
  • MsgBoxStyle_RetryCancel (256): Displays Retry and Cancel buttons.
  • MsgBoxStyle_YesNo (4): Displays Yes and No buttons.
  • MsgBoxStyle_YesNoCancel (3): Displays Yes, No, and Cancel buttons.
  • MsgBoxStyle_DefaultButton1 (0): Sets the first button (usually OK) as the default.
  • MsgBoxStyle_DefaultButton2 (256): Sets the second button (usually Cancel) as the default.
  • MsgBoxStyle_DefaultButton3 (512): Sets the third button (usually Yes) as the default.

Examples

Simple Message Display:

MsgBox "Hello, World!"

Custom Title, Error Icon, OK and Cancel Buttons:

MsgBox "Error: File Not Found", 16 + 256, "File Error"

Input Message with Yes and No Buttons:

response = MsgBox("Do you want to continue?", 4, "Confirmation")
If response = 6 Then
    ' Yes button clicked
Else
    ' No button clicked
End If

Common Issues

  • Empty Message: Ensure the Msg argument is not empty.
  • Invalid Options: Use only valid option values.
  • Expected Value Not Returned: Check if the response variable is properly initialized before using it to store the user’s response.

Integration

MsgBox can be combined with:

  • InputBox to prompt for user input.
  • Select Case statement to handle different user button clicks.
  • Error handling to display error messages and prompt for resolution.
  • InputBox: Prompts for user input.
  • Err object: Provides error information.