Microsoft Access 2010 Bible

(Rick Simeone) #1

Chapter 26: Bulletproofing Access Applications


917


The Button value you provide MsgBox can be a combination of several options. For example, the
following command pops up a message box containing the famous Are you sure? message seen
in many Windows applications. The message box contains Yes, No, and Cancel buttons:

iRetVal = MsgBox(“Are you sure?”, _
vbQuestion + vbYesNoCancel, “Confirm, please”)

Alternatively, a number can be used in place of the VBA intrinsic constants. The following state-
ment is equivalent to the previous example:

iRetVal = MsgBox(“Are you sure”, 35, “Confirm, please”)

The 35 is the sum of vbQuestion (value = 32) and vbYesNoCancel (value = 3). You’ll
find using the VBA intrinsic constants is more self-explanatory. Figure 26.13 shows a variety of dif-
ferent error message boxes. Generally speaking, you’ll want to use the built-in intrinsic constants
(like vbYesNo) instead of their numeric equivalents ( 32 ). Using intrinsic constants always results
in more readable code.

FIGURE 26.13

Message boxes come in a variety of sizes and display a number of different icons.


MsgBox(), like all VBA functions, returns a value. The value returned depends on which button
displayed on the message box is clicked by the user. By default, a message box contains a single
OK button and the return value of the MsgBox() function is 1 when the OK button is clicked.
Not surprisingly, Microsoft provides an intrinsic constant for every value returned by the MsgBox
function. The return values of the different message-box buttons are shown in Table 26.2.

Tip
If you don’t care about which button the user selected, you can use MsgBox as a sub. To do this, simply omit
the parentheses around the arguments passed to MsgBox.


On the CD-ROM
The form named frmMsgBoxDemo in the Chapter26.accdb example database contains a number of differ-
ent message boxes and command buttons. You’ll see how the different VBA constants influence the command
buttons displayed in message boxes and how each type of message box returns a different value.

Free download pdf