MsgBox (function)

<< Click to Display Table of Contents >>

MsgBox (function)

MsgBox(Text [, Type] [, Title])

Displays a message box.

The return value shows which button was pressed to dismiss the message box (see below).

Text is the string to be shown in the message box.

The optional parameter Type indicates which buttons and which icon are displayed in the message box (see below). The default setting is to show only the OK button without any icons.

The optional parameter Title indicates which text will be displayed in the title bar (default value: empty).

See also: Dialog, InputBox

Values for the parameter "Type":

Symbolic constant

Value

Meaning

MB_OK

0

Display only the OK button.

MB_OKCANCEL

1

Display the buttons OK and Cancel.

MB_ABORTRETRYIGNORE

2

Display the buttons Cancel, Retry, Ignore.

MB_YESNOCANCEL

3

Display the buttons Yes, No, Cancel.

MB_YESNO

4

Display the buttons Yes and No.

MB_RETRYCANCEL

5

Display the buttons Retry and Cancel.

MB_ICONSTOP

16

Display the icon for critical messages.

MB_ICONQUESTION

32

Display the icon for questions.

MB_ICONEXCLAMATION

48

Display the icon for warning messages.

MB_ICONINFORMATION

64

Display the icon for informational messages.

MB_DEFBUTTON1

0

Make the first button the default button.

MB_DEFBUTTON2

256

Make the second button the default button.

MB_DEFBUTTON3

512

Make the third button the default button.

MB_APPLMODAL

0

The message box is application-modal. The current task does not accept input until the user closes the message box.

MB_SYSTEMMODAL

4096

The message box is system-modal. The whole system does not accept any input until the user closes the message box (use only for critical errors!).

From each of the four shown groups a value can be chosen. Combine the individual constants by adding their values.

The return values of the MsgBox function

The return value of this function indicates which button was pressed to dismiss the message box:

Symbolic constant

Value

Meaning

IDOK

1

Button OK

IDCANCEL

2

Button Cancel

IDABORT

3

Button Abort

IDRETRY

4

Button Retry

IDIGNORE

5

Button Ignore

IDYES

6

Button Yes

IDNO

7

Button No

Example:

This example uses MsgBox to display a confirmation dialog.

Sub Main

 Dim DgDef, Msg, Response, Title

 Title = "MsgBox Example"

 Msg = "Do you want to continue?"

 DgDef = MB_YESNOCANCEL + MB_ICONQUESTION + MB_DEFBUTTON3

 Response = MsgBox(Msg, DgDef, Title)

 If Response = IDYES Then

         Msg = "You have chosen Yes."

 ElseIf Response = IDCANCEL Then

         Msg = "You have chosen Cancel."

 Else

         Msg = "You have chosen No."

 End If

 MsgBox Msg

End Sub