Examining MsgBox()

Always assign a MsgBox() function to an integer variable. The variable will hold the return value, and that value will indicate the button the user clicked (message boxes can display multiple buttons such as OK and Cancel).

Here is the format of the MsgBox() function:

anIntVariable = MsgBox( strMsg [, [intType] [, strTitle]])

note

The MsgBox() function's format, shown here, accepts one required (strMsg) and two optional (intType and strTitle) arguments. MsgBox() can accept more arguments, but these three are the only ones needed in most applications.

strMsg is a string (either a variable or a string constant enclosed in quotation marks) and forms the text of the message displayed in the message box. intType is an optional numeric value or expression that describes the options you want in the message box. Table 6.1, Table 6.2, and Table 6.3 contain all the possible values you can use for the type of message box you want displayed. (Visual Basic displays no icon if you don't specify an intType value.) If you want to use a value from two or more of the tables, you'll add the values together. Although you can use the integer value, if you use the built-in Visual Basic named literal, you'll more easily understand the message box's style if you ever have to change the message box in the future. strTitle is an optional string that represents the text in the message box's title bar. If you omit strTitle, Visual Basic uses the project's name for the message box's title bar text.

Table 6.1. Select the buttons displayed in the message box.

Named Literal Value Description
vbOKOnly 0 Displays the OK button.
vbOKCancel 1 Displays the OK and Cancel buttons.
vbAbortRetryIgnore 2 Displays the Abort, Retry, and Ignore buttons.
vbYesNoCancel 3 Displays the Yes, No, and Cancel buttons.
vbYesNo 4 Displays the Yes and No buttons.
vbRetryCancel 5 Displays the Retry and Cancel buttons.

Table 6.2. Select the icon displayed in the message box.

Named Literal Value Description
vbCritical 16 Displays Critical Message icon.
vbQuestion 32 Displays Warning Query icon.
vbExclamation 48 Displays Warning Message icon.
vbInformation 64 Displays Information Message icon.
VbSystemModal 4096 Displays a SystemModal dialog box. The user must acknowledge a SystemModal dialog box before doing anything else.

Table 6.3. Select the default button in the message box.

Named Literal Value Description
vbDefaultButton1 0 The first button is the default.
vbDefaultButton2 256 The second button is the default.
vbDefaultButton3 512 The third button is the default.

The options that you select using the intType value in the MsgBox() function determine whether the message box displays an icon and controls the modality of the message box. The modality determines whether a message box is application-specific or system-specific. If it's application-specific, the user must respond to the message box before doing anything else in the application. If the message box is system-specific, the user must respond to the message box before doing anything else on the system.

note

Modality determines how the system handles a dialog box.

The modality often causes confusion. If you don't specify a system-modal intType value of 4096 (or if you don't use the named literal vbSystemModal to specify the system's modal mode), the user's application will not continue until the user closes the message box. However, the user can switch to another Windows program by pressing Alt+Tab or using the application's control menu. If, however, you do specify that the message box is system modal, the user will not be able to switch to another Windows program until the user responds to the message box because the message box will have full control of the system. Reserve the system-modal message boxes for serious error messages that you want the user to read and respond to before continuing the program. You may make a message box a System Modal message box by adding 4096 to the intType value of the message box.

note

If you don't specify an icon, Visual Basic doesn't display an icon. If you don't specify the system modality, Visual Basic assumes that you want an application-modal message box.

tip

If you need to type long VB program statements such as this MsgBox() function, you can break the line into more manageable lines by terminating the first line with an underscore character (_).

Remember that the MsgBox() values such as vbQuestion and vbYesNoCancel are not variables but named literals that Visual Basic has defined to correspond with matching integer values. The named literals vbQuestion and vbYesNoCancel produced both a question mark icon and the three buttons. A title also appeared due to the third value inside the MsgBox() function.

Top Home