|
|
In your programs and macros, you can use message boxes to display information or ask for confirmation of a task. By using an If structure or Select Case structure linked to the value that the message box returns, you can determine which button in the message box the user has chosen. You can use this value to direct the program flow appropriately.
The syntax for displaying a message box with VBA is as follows:
MsgBox(Prompt[, Buttons] [, Title] [, Helpfile], [, HelpContext])
Prompt is the only required argument for the MsgBox statement. Prompt is a string that is displayed as the text in the message box.
Buttons is an optional argument that specifies the buttons that the message box displays, the icons that the message box displays, the default button, and the modality of the message box. Each of these four elements can be specified with either a value or a constant. You use the + operator to join the values or constants together.
Your first choice is the buttons the message should display. Your options are shown in Table 26.3.
Value | Constant | Buttons |
---|---|---|
0 | vbOKOnly | OK (the default if you omit the Buttons argument) |
1 | vbOKCancel | OK, Cancel |
2 | VbAbortRetryIgnore | Abort, Retry, Ignore |
3 | vbYesNoCancel | Yes, No, Cancel |
4 | vbYesNo | Yes, No |
5 | vbRetryCancel | Retry, Cancel |
Next, you can choose which (if any) of the standard four message box icons to display. These choices are shown in Table 26.4.
Value | Constant | Displays |
---|---|---|
16 | vbCritical | Stop icon |
32 | vbQuestion | Question mark icon |
48 | VbExclamation | Exclamation point icon |
64 | VbInformation | Information icon |
Next, you can set a default button for the message box. If you choose not to set a default button, VBA makes the first (leftmost) button in the message box the default—the Yes button in a vbYesNo message box, the OK button in a vbOKCancel message box, and so on. Table 26.5 gives you the choices of default buttons for a message box.
Value | Constant | Default Button |
---|---|---|
0 | vbDefaultButton1 | The first button |
256 | vbDefaultButton2 | The second button |
512 | vbDefaultButton3 | The third button |
768 | vbDefaultButton4 | The fourth button |
In theory, you can choose whether to make the message box application modal (the default) or system modal. Application modality means that you can take no further actions in the application until you dismiss the message box; system modality means you can take no further actions on your computer until you dismiss the message box. In practice, this system modality does not work—even if you use the vbSystemModal argument, the resulting message box will be application modal. (See Table 26.6.)
Value | Constant | Modality |
---|---|---|
0 | vbApplicationModal | Application modal |
4096 | vbSystemModal | System modal |
Finally, you can now specify some new information for your message box. The Help button can now be automatically added, the message box window can be set in the foreground, and two options that are most useful when working with Project in a language that reads from right to left. Text can be right-aligned, and text can read from right to left. (See Table 26.7.)
Value | Constant | Displays |
---|---|---|
16384 | vbMsgBoxHelpButton | Adds Help button to the message box. |
65536 | VbMsgBoxSetFore ground | Specifies the message box window as the foreground window. |
524288 | vbMsgBoxRight | Text is right aligned. |
1048576 | vbMsgBoxRtlReading | Specifies text should appear as right-to-left reading on Hebrew and Arabic systems. |
Title is an optional string argument specifying the text to appear in the title bar of the message box. In most cases, it’s a good idea to specify a Title argument to make it clear which procedure has caused the message box to appear. If you do not specify a Title argument for the message box, VBA will display "Visual Basic" in the title bar, which is singularly uninformative.
Helpfile is an optional argument, specifying the Help file to invoke if the user summons help while the message box is displayed.
Context is an optional argument, specifying the topic to display in the Help file. If you use the Helpfile argument, you must use a corresponding Context argument.
For example, the following statement specifies the message box shown in Figure 26.8. This message box has Yes, No, and Help buttons, with the No button the default, and displays the question mark icon.
MsgBox "Delete all selected tasks?", vbYesNo + vbDefaultButton2 + vbQuestion + vbMsgBoxHelpButton, "Delete Selected Tasks"
Figure 26.8: A custom message box
The previous statements simply display the message boxes described—they don’t let you know which button the user clicked. To return a value from a message box, declare a variable for the value and set the value of the variable to the result of the message box:
Dim intResponse as Integer intResponse = MsgBox "Delete all selected tasks?", vbYesNo
VBA stores the user’s choice of button as a value in the Response variable. Table 26.8 shows the values and their corresponding constants.
Value | Constant | Button Selected |
---|---|---|
1 | vbOK | OK |
2 | vbCancel | Cancel |
3 | vbAbort | Abort |
4 | vbRetry | Retry |
5 | vbIgnore | Ignore |
6 | vbYes | Yes |
7 | vbNo | No |
The following statements use a Select Case structure to establish which button the user chooses in an Abort/Retry/Ignore message box:
Dim AbortRetryIgnore AbortRetryIgnore = MsgBox("Abort, retry, or ignore?", vbAbortRetryIgnore + vbCritical Select Case AbortRetryIgnore Case vbAbort 'Abort the procedure Case vbRetry 'Retry the procedure Case vbIgnore 'Ignore Case Else 'Nothing should produce a Case Else here End Select
For simple Yes/No or OK/Cancel message boxes, you can use a straightforward If statement:
If MsgBox("Is it raining?", vbYesNo + vbQuestion, "Weather Check") Then MsgBox "It's raining." Else MsgBox "It's not raining" End If
The Buttons arguments do not have to be in the order discussed here—they will work in any order. For example, VBScript is smart enough to work out that the 1 in the following statement, rather than either of the 0s, refers to the type of message box:
MsgBox "Proceed?", 0 + 16 + 1 + 0, "Bad Value"
Better yet, you can mix values and constants, if you want; therefore, the following three statements are functionally equivalent:
MsgBox "Proceed?", 1 + 32 + 256 + 4096, "Bad Value"
MsgBox "Proceed?", vbOKCancel + vbQuestion + vbDefaultButton2 + vbSystemModal,
"Bad Value"
MsgBox "Proceed?", 4096 + 32 + vbOKCancel + 256, "Bad Value"
That said, your code will be easier to read if you use the standard order: Type of message box, icon, default button, and then modality. It will also be easier to read—though a little longer—if you use the constants rather than the values.
|
|