Access VBA Macro Programming

(Joao Candeias) #1
Following is an example of the message box with Yes and No buttons and a defined
caption:

MsgBox "Test message", vbYesNo, "My message"

This will display Yes and No buttons.
You can combine icon and button constants with theOroperator:

x = MsgBox("Test Message", vbAbortRetryIgnore Or vbCritical)

This will display a message box with the Abort, Retry, and Ignore buttons and a Critical
message icon.
Displaying the buttons is relatively easy, but how do you detect when the user clicks a
particular button? You still need to write code to deal with the button that has been clicked.
You do this by collecting the response in a variable:

x = MsgBox ("Test Yes No",vbYesNo,"Test")
Msgbox x

Note that in this instance you usex =and put parentheses around the parameters. Without the
parentheses, you will get an error because you are calling a function, which needs the parentheses
to show the parameters.

68 Microsoft Access 2010 VBA Macro Programming


Constant Value Description
vbAbortRetryIgnore 2 Displays the Abort, Retry, and Ignore Buttons.
vbCritical 16 Displays the Stop icon—a white cross on a red circle.
vbDefaultButton1 0 First Button is the default button.
vbDefaultButton2 256 Second Button is the default button.
vbDefaultButton3 512 Third Button is the default button.
vbDefaultButton4 768 Fourth Button is the default button.
vbExclamation 48 Displays the Exclamation icon.
vbInformation 64 Displays the Information icon.
vbOKCancel 1 Displays the OK and Cancel buttons.
vbOKOnly 0 Displays the OK button only.
vbQuestion 32 Displays the Question icon.
vbRetryCancel 5 Displays the Retry and Cancel Buttons.
vbYesNo 4 Displays the Yes and No buttons.
vbYesNoCancel 3 Displays the Yes, No, and Cancel buttons.
Table 5-8 Constants for Message Boxes
Free download pdf