Advanced: Requires expert coding, interoperability, and multiuser skills.
File dialog box and to determine the files that the user selects.
Note The FileDialog method works only in the full retail version of Microsoft Access. This method does not work in a Microsoft Access run-time application.
By using the FileDialog method in Microsoft Office Access 2003, you can display the
File dialog box that is used by Microsoft Access and to determine the files that the user selects. The SelectedItems collection of the FileDialog object contains the paths to the files that are selected by the user. By using a For-Each loop, you can enumerate this collection and then display each file. The following example loops through the ItemsSelected collection and then displays files in the list box.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
- Start Microsoft Access.
- Open the sample database Northwind.mdb.
- In the Database window, click Forms under
- In the right pane, double-click Create form in Design view.
- Add the following controls to the form:
Caption: Add Files
OnClick: [Event Procedure]
RowSourceType: Value List
- On the View menu, click
Code to open the module of the form in the Microsoft Visual Basic Editor.
- On the Tools menu, click
- In the
References-DatabaseName dialog box, click to select the Microsoft Office 11.0 Object Library check box, and then click OK.
- Add the following code to the module of the form:
Option Compare Database
Private Sub cmdFileDialog_Click()
' This requires a reference to the Microsoft Office 11.0 Object Library.
Dim fDialog As Office.FileDialog
Dim varFile As Variant
' Clear the list box contents.
Me.FileList.RowSource = ""
' Set up the File dialog box.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
' Allow the user to make multiple selections in the dialog box.
.AllowMultiSelect = True
' Set the title of the dialog box.
.Title = "Select One or More Files"
' Clear out the current filters, and then add your own.
.Filters.Add "Access Databases", "*.MDB"
.Filters.Add "Access Projects", "*.ADP"
.Filters.Add "All Files", "*.*"
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
' Loop through each file that is selected and then add it to the list box.
For Each varFile In .SelectedItems
MsgBox "You clicked Cancel in the file dialog box."
- Save the form as Form1, and then close it.
- In the Database window, select Form1, and then click Open to open the form in the Form view.
- Click Add Files. The Select One or More Files dialog box appears.
- Select one or more files, and then click
OK, or click Cancel.
Help menu, type filedialog object in the Search for box in the Assistance pane, and then click
Start searching to view the topic.
Article ID: 824272 - Last Review: Sep 17, 2011 - Revision: 1