MsgBox Function, MsgBox Statement
See Also Example
Displays a message in a dialog box and waits for the user to choose a button. The MsgBox function returns a value indicating which button the user has chosen; the MsgBox statement does not.
MsgBox(msg [, [type][, title] ] )
MsgBox msg [, [type][, title] ]
The MsgBox function and statement both have these parts:
msg String expression displayed as the message in the dialog box.
type Optional numeric expression that is the sum of values specifying the number and type of buttons to display, the icon style to use, the identity of the default button, and the modality. The following table illustrates the values used and the meaning of each group of values:
Symbolic constant Value Meaning
MB_OK 0 Display OK button only.
MB_OKCANCEL 1 Display OK and Cancel buttons.
MB_ABORTRETRYIGNORE 2 Display Abort, Retry, and Ignore buttons.
MB_YESNOCANCEL 3 Display Yes, No, and Cancel buttons.
MB_YESNO 4 Display Yes and No buttons.
MB_RETRYCANCEL 5 Display Retry and Cancel buttons.
MB_DEFBUTTON1 0 First button is default.
MB_DEFBUTTON2 256 Second button is default.
MB_DEFBUTTON3 512 Third button is default.
MB_APPLMODAL 0 Application modal. The user must respond to the message box before continuing work in the current application.
MB_SYSTEMMODAL 4096 System modal. All applications are suspended until the user responds to the message box.
The first group of values (1-5) describes the number and type of buttons displayed in the dialog box; the second group (16, 32, 48, 64) describes the icon style; the third group (0, 256, 512) determines which button is the default; and the fourth group (0, 4096) determines the modality of the message box. When adding numbers to create a final value for the argument type, use only one number from each group. If omitted, the default value for type is 0.
title String expression displayed in the title bar of the dialog box. If you omit the argument title, MsgBox uses "Microsoft Visual Basic" as the default title for applications running in the Visual Basic development environment and the application name for executable files created by Visual Basic.
For application modal message boxes, MsgBox displays a maximum of 1024 characters. Longer messages are truncated after the 1024th character. Message strings longer than 255 characters with no intervening spaces are truncated after the 255th character. For system modal message boxes, the number of characters you can display depends on screen resolution and whether or not the string to be displayed is on one or more lines.
MsgBox breaks lines automatically at the right edge of the dialog box. If you want to set line breaks yourself, place a linefeed (ANSI character 10) before the first character of the text that is to begin each new line. For system modal message boxes in Microsoft Windows 3.0 only, the linefeed character does not cause a line to wrap.
The value returned by the MsgBox function indicates which button has been selected, as shown in the following table:
Symbolic constant Value Meaning
IDOK 1 OK button selected.
IDCANCEL 2 Cancel button selected.
IDABORT 3 Abort button selected.
IDRETRY 4 Retry button selected.
IDIGNORE 5 Ignore button selected.
IDYES 6 Yes button selected.
IDNO 7 No button selected.
If the dialog box displays a Cancel button, pressing the Esc key has the same effect as choosing Cancel.
Note Symbolic constants for all message box definitions can be found in the Visual Basic file CONSTANT.TXT. When placed in any module in a project, the symbolic names can be used in all your form and code modules.
InputBox, InputBox$ Function
MsgBox Function, MsgBox Statement Example
The example uses MsgBox to display a critical-error message in a dialog box with a Yes button and a No button. The No button is the default response. The MsgBox function returns a value based on the button chosen by the user. The MsgBox statement uses that value to display a message that indicates which button was chosen. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Const MB_OK = 0, MB_OKCANCEL = 1 ' Define buttons.
Const MB_YESNOCANCEL = 3, MB_YESNO = 4
Const MB_ICONSTOP = 16, MB_ICONQUESTION = 32 ' Define Icons.
Const MB_ICONEXCLAMATION = 48, MB_ICONINFORMATION = 64
Const MB_DEFBUTTON2 = 256, IDYES = 6, IDNO = 7 ' Define other.
Dim DgDef, Msg, Response, Title ' Declare variables.
Title = "MsgBox Demo"
' Put together a sample message box with all the proper components.
Msg = "This is a sample of a critical-error message."
Msg = Msg & " Do you want to continue?"
DgDef = MB_YESNO + MB_ICONSTOP + MB_DEFBUTTON2 ' Describe dialog.
Response = MsgBox(Msg, DgDef, Title) ' Get user response.
If Response = IDYES Then ' Evaluate response
Msg = "You chose Yes." ' and take appropriate
Else ' action.
Msg = "You chose No or pressed Enter."
MsgBox Msg ' Display action taken.