Using the MsgBox Builder

msgbox code 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. vbAbortRetryIgnore. 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.

msgbox code builder

Simple MsgBox call - vbOKOnly

MsgBox Buttons:=vbInformation, 
                Prompt:="some information"

In the MsgBox call the Buttons:= value 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 VbMsgBoxResult), the required action as indicated by the user by pressing one of the two buttons: vbOK or vbCancel The Select Case corresponginly has two branches: in Case vbOK the procedure continues, in 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).

The extra + 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


Select Case MsgBox(Buttons:=vbAbortRetryIgnore + vbCritical, Prompt:="Appliance not found")
Case vbAbort
    GoTo HandleExit
Case vbRetry
    GoTo Retry
Case vbIgnore

End Select


This case is similar to the case above (vbOKCancel): vbAbort takes the place of vbCancel and vbIgnore that of vbOK. The current code adds a third branch: Case vbRetry. Here, the control flow is back to an earlier place in the code indicated by label Retry:. 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.

The extra + 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

This case is similar to vbOKCancel, except for the additional branche Case vbNo, 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 Case vbCancel

Specifying Prompt, Icon, Title and default button

msgbox code builder

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:

  1. enter your prompt text,
  2. change the icon,
  3. change the default text (the application name) in the title bar,
  4. select which button you want to be the preselected default.