Using the MsgBox Builder
The VBA MsgBox procedure is used in many ways to communicate with the end-user: it both informs the user, and allows the user to decide how to continue. The latter follows a fixed set of patterns in code which I will discuss first. It is here that the MsgBox Builder has its primary use for the programmer. I finish by showing how the user interface allows you in the most intuitive way specify the MsgBox arguments.
Code for different uses of the MsgBox
The above screenshot shows the MsgBox builder dialog where the user selects a most suitable use -
corresponding to the constant select for the
Buttons argument, e.g.
There are six such constants and the list box in the MsgBox Builder produces code that handles the 'scenario' you select.
I will now present the code fragments that will be inserted for each use case.
|Alt-CTB | Menu: Code VBA » Dialog » MMessageBox Builder|
Simple MsgBox call - vbOKOnly
MsgBox Buttons:=vbInformation, Prompt:="some information"
In the MsgBox call the
vbOKOnly is not mentioned because it is the default.
It does however include
vbInformation, which makes the 'Information' icon appear, telling the user the message's purpose to be informative only.
MsgBox - vbOKCancel
Select Case MsgBox(Buttons:=vbOKCancel + vbExclamation, Prompt:="some information") Case vbOK Case vbCancel GoTo HandleExit End Select
In the MsgBox call the
Buttons:=vbOKCancel makes the Cancel button available.
The message box now has two possible outcomes (type
the required action as indicated by the user by pressing one of the two buttons:
Select Case corresponginly has two branches: in
Case vbOK the procedure continues,
Case vbCancel is followed by
GoTo HandleExit which will jump to label
HandleExit: at the end of the procedure.
or replace the
GoTo HandleExit by
Exit Sub (or Function).
+ vbExclamation, which makes the 'Exclamation' icon appear, indicating to the user the message is not informative only,
but requires a decision from his side: to proceed or not.
MsgBox - vbAbortRetryIgnore
Retry: Select Case MsgBox(Buttons:=vbAbortRetryIgnore + vbCritical, Prompt:="Appliance not found") Case vbAbort GoTo HandleExit Case vbRetry GoTo Retry Case vbIgnore End Select HandleExit:
This case is similar to the case above (vbOKCancel):
vbAbort takes the place of
vbIgnore that of
The current code adds a third branch:
Here, the control flow is back to an earlier place in the code indicated by label
You will have to place the
Retry: just before whatever you want to give another chance,
e.g. entering a new Appliance name the program can look for in the database.
+ vbCritical, which makes the 'Critical issue' icon appear,
indicating to the user has fix something lest he be able to proceed.
MsgBox - vbYesNoCancel, vbYesNo
Select Case MsgBox(Buttons:=vbYesNoCancel + vbExclamation, Prompt:="") Case vbYes Case vbNo Case vbCancel GoTo HandleExit End Select HandleExit:
This case is similar to vbOKCancel, except for the additional branche
and the other use of the Buttons argument, and the resulting captions- Yes and No on them.
Here, the builder has chosen to add
vbExclamation bidding the user to make a conscious decision.
Selecting YesNo gives the simpler
Select Case, without the extra
Specifying Prompt, Icon, Title and default button
Finally, as the animation shows, the MsgBox builder lets you specify the other characteristics of your mmessage box visually. Check in the code area how the arguments are changed automatically when you make changes:
- enter your prompt text,
- change the icon,
- change the default text (the application name) in the title bar,
- select which button you want to be the preselected default.