Using a Common Dialog control

[Common Dialog control in Access toolbox] Sometimes you will want to let the user save a file to disk, for example when exporting values to an Excel spreadsheet. You could ask the user to type the file name into a textbox but you've then got to rely on them typing the path and name in properly. A better solution is to use the Microsoft Common Dialog control. This lets the user navigate through drives and folders and select the file that they want.

The Common Dialog control does not appear in the standard Access toolbox. Select the 'More Controls' button at the end of the toolbar, pick 'Microsoft Common Dialog Control' from the list that appears, and drop it onto the form.

[Common Dialog Control on a Microft Access form] The Common Dialog control looks like a button at design time. Don't worry about this, the control is invisible at run time and the user won't see it. The control only exists as a container for the methods and properties of the dialogs.

Add this code to the Click event of the button:

With CommonDialog
  '-- Start in c:\ and show the Save dialog
  .InitDir = "c:\"
  .DialogTitle = "Export to Excel"
  '-- Did the user select a file name?
  If .FileName = "" Then
    MsgBox "Please enter a file name"
    '-- Put some code here to save the file
    MsgBox "File saved as " & .FileName
  End If
End With

This code will show a standard Windows file-selection dialog to the user. The filename is saved in the FileName property of the control. This filename will be empty if the user presses Cancel or clicks the close widget of the dialog.


The Common Dialog Control is not part of Access and some of the properties might be unfamiliar. For example, you set DialogTitle instead of Caption and you call ShowSave to make the dialog appear instead of setting Visible = True.

These properties appear in the Access Properties window as for any other control but if you click on the three dots by the Custom property on the Other tab then this custom dialog will appear to let you set the most useful properties:

[Common Dialog Properties dialog in Microsoft Access]

The Flags property of the Common Dialog control alters the behaviour of the dialog. For example:

.Flags = cdlOFNOverwritePrompt

will display a "Do you want to replace this file?" message box to warn the user if the file named already exists. The full list of flags is given in Access Help.

Other dialogs

The custom properties dialog has other tabs "Color", "Font", "Print". You can use the ShowColor method of the Common Dialog control to let the user select a colour or ShowPrinter to let the user choose and set up a printer. The Common Dialog control makes it easy for you to give your users a lot more choice without having to do too much programming yourself. It can also make your Access database look more professional because these are standard Windows dialogs and the user will find them familiar.

The final thing to remember with all versions of the Common Dialog control is that it will not save a file or change a colour by itself. All that it does is get you the file name or the colour chosen by the user. You as the developer still have to write the VBA code to respond to that choice.