The Msg Box Function

We have been using the MsgBox function unofficially for some time now. Let us introduce it officially. The MsgBox function is used to display a message and wait for the user to respond by pushing a button. The most commonly used syntax is:

This is not the function's complete syntax. There are some additional optional parameters related to help contexts that you can look up in the help documentation.

prompt is a String parameter containing the message to be displayed in the dialog box. Note that a multiline message can be created by interspersing the vbCrLf constant within the message.

buttons is a Long parameter giving the sum of values that specify various properties of the message box. These properties are the number and type of buttons to display, the icon style to use, the identity of the default button, and the modality of the message box. (A system modal dialog box remains on top of all currently open windows and captures the input focus systemwide, whereas an application modal dialog box remains on top of the application's windows only and captures the application's focus.) The various values of Buttons that we can sum are shown in Table 7-2. (They are officially defined in the VbMsgBoxStyle enum.)

Table 7-2. The MsgBox Buttons Argument Values

Purpose

Constant

Value

Description

Button types

vbOKOnly

0

Display OK button only

vbOKCancel

1

Display OK and Cancel buttons

vbAbortRetryIgnore

2

Display Abort, Retry, and Ignore buttons

vbYesNoCancel

3

Display Yes, No, and Cancel buttons

vbYesNo

4

Display Yes and No buttons

vbRetryCancel

5

Display Retry and Cancel buttons

Icon types

vbCritical

16

Display Critical Message icon

vbQuestion

32

Display Warning Query icon

vbExclamation

48

Display Warning Message icon

vbInformation

64

Display Information Message icon

Default button

vbDefaultButtonl

0

First button is default

vbDefaultButton2

256

Second button is default

vbDefaultButton3

512

Third button is default

vbDefaultButton4

768

Fourth button is default

Modality

vbApplicationModal

0

Application modal message box

vbSystemModal

4096

System modal message box

For instance, the code:

MsgBox "Proceed?", vbQuestion + vbYesNo displays the message box shown in Figure 7-1, which includes a question mark icon and two command buttons, labeled Yes and No.

Figure 7-1. A MsgBox dialog box

Figure 7-1. A MsgBox dialog box

The title parameter is a string expression that is displayed in the title bar of the dialog box. If we omit this argument, then "Microsoft Excel" will be displayed, as in Figure 7-1.

The MsgBox function returns a number indicating which button was selected. These return values are given in Table 7-3. (They are officially defined in the VbMsgBoxResult enum.)

Table 7-3. MsgBox Return Values

Constant

Value

Description

vbOK

1

OK button pressed

vbCancel

2

Cancel button pressed

vbAbort

3

Abort button pressed

vbRetry

4

Retry button pressed

vblgnore

5

Ignore button pressed

vbYes

6

Yes button pressed

vbNo

7

No button pressed

0 0

Post a comment