Create a Form, add a Listbox control and rename it "FileList".
Add a button to a Form.
Set the OnClick() Event of the Button.
In the VBA Editor go to Tools | References.
Find the Microsoft Office 11.0 Object Library Reference and add it.
This maybe a higher number depending which version of Office you have installed.
Code:
Private Sub cmdFileDialog_Click()
' Requires reference to Microsoft Office 11.0 Object Library.
Dim fDialog As Office.FileDialog
'Dim fDialog As Object
Dim varFile As Variant
' Clear listbox contents.
Me.FileList.RowSource = ""
' Set up the File Dialog.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
'Set fDialog = Application.FileDialog(3)
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 With
End Sub
You might get the following error using the Office.FileDialog so just swap it to the below.
Compile error: User-defined type not defined
'Dim fDialog As Office.FileDialog
Dim fDialog As Object
'Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
Set fDialog = Application.FileDialog(3)
There are 4 options you can choose from as a Dialog but the msoFileDialogOpen and msoFileDialogSaveAs constants are not supported in Microsoft Access.
ObjectType (MsoFileDialogType)
Name
Value
Description
msoFileDialogFilePicker
3
File picker dialog box.
msoFileDialogFolderPicker
4
Folder picker dialog box.
msoFileDialogOpen
1
Open dialog box.
msoFileDialogSaveAs
2
Save As dialog box.
If you use the Folder Picker you can't use the Filters.
There are other options you can set.
With fDialog
...
.InitialView = msoFileDialogViewSmallIcons
End With
ObjectType (MsoFileDialogType)
Name
Value
Description
msoFileDialogViewDetails
2
Files displayed in a list with detail information.
msoFileDialogViewLargeIcons
6
Files displayed as large icons.
msoFileDialogViewList
1
Files displayed in a list without details.
msoFileDialogViewPreview
4
Files displayed in a list with a preview pane showing the selected file.
msoFileDialogViewProperties
3
Files displayed in a list with a pane showing the selected file's properties.
msoFileDialogViewSmallIcons
7
Files displayed as small icons.
msoFileDialogViewThumbnail
5
Files displayed as thumbnails.
msoFileDialogViewTiles
9
Files displayed as tiled icons.
msoFileDialogViewWebView
8
Files displayed in Web view.
You can also set which Folder will open:
With fDialog
...
.InitialFileName = "c:\599CD\"
End With
You can change which Filter is selected if there is more than 1.
With fDialog
...
.FilterIndex = 1
End With
You can also change the text on the OK Button.
With fDialog
...
.ButtonName = "Choose &Access file"
End With