This article was previously published under Q279508
For a Microsoft Office Access 2003 version of this article, see 824272.
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).
This article shows you how to use the new FileDialog method in Microsoft Access to display the built-in file dialog box and to determine what files the user selected.
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.
In earlier versions of Microsoft Access, you could not display the file dialog box without using either the Microsoft Common Dialog ActiveX Control or by making calls to the Windows API.
In Microsoft Access 2002, the FileDialog method allows you to display the file dialog box used by Microsoft Access and to determine what files were selected by the user. The SelectedItems collection of the FileDialog object contains the paths to the files selected by the user. By using a For...Each loop, you can enumerate this collection and display each file. The following example loops through the ItemsSelected collection and displays the files in a 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.
Open the sample database Northwind.mdb.
Create a new blank form in Design view.
Add the following controls to the form:
Command button -------------------------- Name: cmdFileDialog Caption: Add Files OnClick: [Event Procedure] List box ------------------------- Name: FileList RowSourceType: Value List
On the View menu, click Code to open the form's module in the Visual Basic Editor.
On the Tools menu, click References.
Click to select the Microsoft Office 10.0 Object Library check box.
Click OK to close the References dialog box.
Add the following code to the form's module:
Option Compare DatabaseOption Explicit Private Sub cmdFileDialog_Click()'Requires reference to Microsoft Office 10.0 Object Library. Dim fDialog As Office.FileDialog Dim varFile As Variant 'Clear listbox contents. Me.FileList.RowSource = "" 'Set up the File Dialog. Set fDialog = Application.FileDialog(msoFileDialogFilePicker) With fDialog 'Allow user to make multiple selections in dialog box .AllowMultiSelect = True 'Set the title of the dialog box. .Title = "Please select one or more files" 'Clear out the current filters, and add our own. .Filters.Clear .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 selected and add it to our list box. For Each varFile In .SelectedItems Me.FileList.AddItem varFile Next Else MsgBox "You clicked Cancel in the file dialog box." End If End WithEnd Sub
Save the form as Form1, and then close it.
Open the Form1 form in Form view.
Click Add Files. Note that the Please select one or more files dialog box appears.
Select one or more files, and then click OK, or click Cancel.
If you selected one or more files, note that those file names appear in the list box. If you clicked Cancel, note that you receive a message indicating that you clicked Cancel.
For more information about what you can do with the file dialog box, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type filedialog object in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
OfficeKBHowTo inf open saveas save as file picker folder