Displays a message in a dialog box and waits for the user to choose a button.
msg
The string displayed in the dialog box as the message.
type
Optional parameter. Designates the type of button.
title
Optional parameter. Designates the dialog box title. If you omit the argument title, MsgBox has no default title.
The
MsgBox function returns a value indicating which button the user has selected. The
MsgBox statement does not.
Value |
Meaning |
0 |
Display OK button only. |
1 |
Display OK and Cancel buttons. |
2 |
Display Abort, Retry, and Ignore buttons. |
3 |
Display Yes, No, and Cancel buttons. |
4 |
Display Yes and No buttons. |
5 |
Display Retry and Cancel buttons. |
16 |
Stop Icon |
32 |
Question Icon |
48 |
Exclamation Icon |
64 |
Information Icon |
0 |
First button is default. |
256 |
Second button is default. |
512 |
Third button is default. |
768 |
Fourth button is default |
0 |
Application modal |
4096 |
System modal |
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.
The value returned by the
MsgBox function indicates which button has been selected, as shown below:
Value |
Meaning |
1 |
OK button selected. |
2 |
Cancel button selected. |
3 |
Abort button selected. |
4 |
Retry button selected. |
5 |
Ignore button selected. |
6 |
Yes button selected. |
7 |
No button selected. |
If the dialog box displays a Cancel button, pressing the Esc key has the same effect as choosing Cancel.
MsgBox Function, MsgBox Statement Example
The example uses
MsgBox to display a close without saving message in a dialog box with a Yes button a No button and a Cancel button. The Cancel 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.
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Do you want to continue ?" ' Define message
'Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons
Style = 4 + 16 + 256 ' Define buttons.
Title = "MsgBox Demonstration" ' Define title
Help = "DEMO.HLP" ' Define Help file
Ctxt = 1000 ' Define topic
' context.
' Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' User chose Yes
MyString = "Yes" ' Perform some action
Else ' User chose No.
MyString = "No" ' Perform some action
End If