tags:

views:

7559

answers:

3

How would I go about showing an open file (or file select) dialog in access 2007 VBA?

I have tried using Application.GetOpenFileName as I would in Excel, but this function doesn't exist in Access.

+2  A: 

Please see How to use the Common Dialog API in a database in Access 2003 or Access 2007

If you want finer grain control, you will need to use the Win32 API directly: Call the standard Windows File Open/Save dialog box

Mitch Wheat
Would the downvoter please leave a comment. Thanks.
Mitch Wheat
No downvoter explanation? I think it's a good answer. +1
HansUp
+2  A: 

In Access 2007 you just need to use Application.FileDialog.

Here is the example from the Access documentation:

' Requires reference to Microsoft Office 12.0 Object Library. '
Private Sub cmdFileDialog_Click()
   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 With
End Sub

As the sample says, just make sure you have a reference to the Microsoft Access 12.0 Object Library (under the VBE IDE > Tools > References menu).

Renaud Bompuis
Albert D. Kallal
+3  A: 

My comments messed up in above.

Actually, you can use late binding, and the reference to the 11.0 object library is not required.

The following code will work without any references:

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

 MsgBox "file choosen = " & f.SelectedItems.Count

Note that the above works well I the runtime also.

Albert D. Kallal