Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the browse for file dialog in Access VBA

Tags:

vba

ms-access

i saw this topic How to show "Open File" Dialog in Access 2007 VBA? and i like the solution there that's not using references, however, i can't figure out how to display the file path that the user selected. can someone please explain

thank you very much

this is the piece i'm talking about

Dim f As Object   
Set f = Application.FileDialog(3)   
f.AllowMultiSelect = True   
f.Show    
MsgBox "file choosen = " & f.SelectedItems.Count
like image 555
lalachka Avatar asked Jan 27 '11 07:01

lalachka


1 Answers

First things first: you should always prefer to use strongly-typed variables whenever possible. In this case, you can replace Object with Office.FileDialog.

To display the paths of each file that was selected, you need to loop through the SelectedItems collection. For example, you would add the following code:

Dim f As Office.FileDialog
Set f = Application.FileDialog(3)   
f.AllowMultiSelect = True

' Show the dialog. If the method returns True, the user picked at least one file.
' If the method returns False, the user clicked Cancel.
If f.Show Then
    MsgBox f.SelectedItems.Count & " file(s) were chosen."

    ' Display the full path to each file that was selected
    Dim i As Integer
    For i = 1 To f.SelectedItems.Count
        MsgBox f.SelectedItems(i)
    Next i
End If

Note that the FileDialog also has other properties that you can set, if you require customization. For example, the .Title property allows you to specify a title that will appear as the dialog's caption in the title bar. You can also specify a filter using the .Filter property, which will limit the type of files that the user will be able to see and choose from in the dialog. For example, if you wanted to limit the choices to only Access Databases, you could add the following code:

' Clear out the current filters
f.Filters.Clear

' Add a few custom filters
f.Filters.Add "Access Databases", "*.mdb"
f.Filters.Add "All Files", "*.*"
like image 57
Cody Gray Avatar answered Sep 22 '22 13:09

Cody Gray