Using Built-In Dialog Boxes
Access comes with two built-in dialog boxes: the standard Windows message box and the input box. The FileDialog object introduced with Access 2002 gives you access to other commonly used dialog boxes.
Message Boxes
A message box is a predefined dialog box that you can incorporate into your applications; however, you can customize it by using parameters. The VBA language has a MsgBox statementthat just displays a messageand a MsgBox function, which can display a message and return a value based on the user's response.The message box in the VBA language is the same message box that's standard in most Windows applications, so it's already familiar to most Windows users. Rather than create your own dialog boxes to get standard responses from your users, you can use an existing, standard interface.
The MsgBox Function
The MsgBox function receives five parameters. The first parameter is the message that you want to display. The second is a numeric value indicating which buttons and icon you want to display. Tables 9.1 and 9.2 list the values that you can numerically add to create the second parameter. You can substitute the intrinsic constants in the table for the numeric values, if you want.MsgBox's third parameter is the message box's title. Its fourth and fifth parameters are the Help file and context ID that you want available if the user selects Help while the dialog box is displayed. The MsgBox function syntax looks like this:MsgBox "This is a Message", vbInformation, "This is a Title"
This example displays the message "This is a Message" and the information icon. The title for the message box is "This is a Title". The message box also has an OK button that's used to close the dialog box.The MsgBox function is normally used to display just an OK button, but it can also be used to allow a user to select from a variety of standard button combinations. When used in this way, it returns a value indicating which button the user selected.
Buttons | Value | Intrinsic Constant |
---|---|---|
OK button only | 0 | vbOKOnly |
OK and Cancel | 1 | vbOKCancel |
Abort, Retry, and Ignore | 2 | vbAbortRetryIgnore |
Yes, No, and Cancel | 3 | vbYesNoCancel |
Yes and No | 4 | vbYesNo |
Retry and Cancel | 5 | vbRetryCancel |
Icon | Value | Intrinsic Constant |
---|---|---|
Critical (Stop Sign) | 16 | vbCritical |
Warning Query (Question) | 32 | vbQuestion |
Warning Exclamation (!) | 48 | vbExclamation |
Information (I) | 64 | vbInformation |
Dim intAnswer As Integer
intAnswer = MsgBox("Are You Sure?", vbYesNoCancel + vbQuestion, _
"Please Respond")
End Sub
This message box also displays the Question icon (see Figure 9.12). The Function call returns a value stored in the Integer variable intAnswer.
Figure 9.12. The dialog box displayed by the MsgBox function.

After you have placed the return value into a variable, you can easily introduce logic into your program to respond to the user's selection, as shown in this example:Sub MessageBoxAnswer()
Dim intAnswer As Integer
intAnswer = MsgBox("Are You Sure?", vbYesNoCancel + vbQuestion, _
"Please Respond")
Select Case intAnswer
Case vbYes
MsgBox "I'm Glad You are Sure!!"
Case vbNo
MsgBox "Why Aren't You Sure??"
Case vbCancel
MsgBox "You Coward! You Bailed Out!!"
End Select
End Sub
This code evaluates the user's response and displays a message based on her answer. Of course, in a real-life situation, the code in the Case statements would be more practical. Table 9.3 lists the values returned from the MsgBox function, depending on which button the user selected.
Response | Value | Intrinsic Constant |
---|---|---|
OK | 1 | vbOK |
Cancel | 2 | vbCancel |
Abort | 3 | vbAbort |
Retry | 4 | vbRetry |
Ignore | 5 | vbIgnore |
Yes | 6 | vbYes |
No | 7 | vbNo |
Input Boxes
The InputBox function displays a dialog box containing a simple text box. It returns the text that the user typed in the text box and looks like this:Sub InputBoxExample()
Dim strName As String
strName = InputBox("What is Your Name?", _
"This is the Title", "This is the Default")
MsgBox "You Entered " & strName
End Sub
This subroutine displays the input box shown in Figure 9.13. Notice that the first parameter is the message, the second is the title, and the third is the default value. The second and third parameters are optional.
Figure 9.13. An example of using the InputBox function to gather information.

The FileDialog Object
The FileDialog object was introduced with Access 2002. This object allows you to easily display the common dialog boxes previously available only by using the Common Dialog ActiveX control. Here's an example of how FileDialog works:Sub SaveDialog()
'Declare a FileDialog object
Dim dlgSaveAs As FileDialog
'Instantiate the FileDialog object
'indicating that it will act as a File SaveAs dialog
Set dlgSaveAs = Application.FileDialog( _
DialogType:=msoFileDialogSaveAs)
'Display the dialog
dlgSaveAs.Show
'Display the specified file name in a message box
MsgBox dlgSaveAs.SelectedItems(1)
End Sub
The code in the example declares a FileDialog object. It instantiates the object, setting its type to a File Save As dialog box. It shows the dialog box and then displays the first selected file in a message box. Here's another example:Sub OpenDialog()
'Declare a FileDialog object
Dim dlgOpen As FileDialog
'Instantiate the FileDialog object, setting its
'type to a File Open dialog
Set dlgOpen = Application.FileDialog( _
DialogType:=msoFileDialogOpen)
With dlgOpen
'Allow multiple selections in the dialog
.AllowMultiSelect = True
'Display the dialog
.Show
End With
'Display the first file selected in the dialog
MsgBox dlgOpen.SelectedItems(1)
End Sub
This code once again declares a FileDialog object. When the code instantiates the object, it designates the dialog box type as a File Open dialog box. It sets the AllowMultiSelect property of the dialog box to allow multiple selections in the dialog. It displays the dialog box, and then displays the first selected file in a message box.