In earlier versions of Microsoft Access, you may display the
file dialog box by using either the Microsoft
Common Dialog ActiveX control or by making calls to the Windows API.
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
Objects.
- In the right pane, double-click Create 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 module of the form in the Microsoft Visual
Basic Editor.
- On the Tools menu, click
References.
- 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
Option Explicit
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)
With fDialog
' 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.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 that is selected and then add it to the 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 With
End Sub
- 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.
If you select one or more files, you may notice that the file
names appear in the list box. If you click
Cancel, you may
receive a message that indicates that you clicked
Cancel.