37

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.

braX
  • 11,506
  • 5
  • 20
  • 33
jwoolard
  • 6,024
  • 9
  • 37
  • 37

5 Answers5

49

My comments on Renaud Bompuis's answer messed up.

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 in the runtime also.

eksortso
  • 1,273
  • 3
  • 12
  • 21
Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • 4
    +1 Always hoped this was possible, the key to making the late binding work is passing the numeric option rather than msoOpenFileDialog etc. So simple yet a great answer : ) – Matt Donnan Jul 26 '12 at 19:48
  • You can, but shouldn't. With late binding you are coding in the dark.. When you add the reference and properly declare and set your filedialog object the IDE shows you hints, whereas with late binding it will not. – Chris Jan 12 '15 at 16:03
  • 1
    @Chris OTOH, using early binding means your users have to have the references added as well. That means that late binding is necessary for some applications. – Charles Wood Oct 19 '15 at 13:43
19

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
  • 16,596
  • 4
  • 56
  • 86
  • 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 Edmonton, Alberta Canada kallal@msn.com – Albert D. Kallal Jul 09 '09 at 02:25
3

Addition to what Albert has already said:

This code (a mashup of various samples) provides the ability to have a SaveAs dialog box

Function getFileName() As String
    Dim fDialog    As Object
    Set fDialog = Application.FileDialog(msoFileDialogSaveAs)
    Dim varFile As Variant

    With fDialog
       .AllowMultiSelect = False
       .Title = "Select File Location to Export XLSx :"
       .InitialFileName = "jeffatwood.xlsx"

    If .Show = True Then
       For Each varFile In .SelectedItems
         getFileName = varFile
       Next
    End If
End With
End Function
John M
  • 14,338
  • 29
  • 91
  • 143
  • 1
    This is just code for the same answer that everybody else has given. It's more verbose and shows more options, but it's just a repeat of existing answers. – David-W-Fenton Jul 07 '11 at 20:07
  • @David-W-Fenton No, it's not. It returns a filename and allows you to set a filter, which neither of the other answers do. It's free of Office dependencies and can be pasted directly into any form or module, so it's plug and play. This is the better answer, and as for the other answers your critique is even more apt for people who copy/paste from documentation. – Henrik Erlandsson Oct 02 '12 at 06:42
2

I have a similar solution to the above and it works for opening, saving, file selecting. I paste it into its own module and use in all the Access DB's I create. As the code states it requires Microsoft Office 14.0 Object Library. Just another option I suppose:

Public Function Select_File(InitPath, ActionType, FileType)
    ' Requires reference to Microsoft Office 14.0 Object Library.

    Dim fDialog As Office.FileDialog
    Dim varFile As Variant


    If ActionType = "FilePicker" Then
        Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
        ' Set up the File Dialog.
    End If
    If ActionType = "SaveAs" Then
        Set fDialog = Application.FileDialog(msoFileDialogSaveAs)
    End If
    If ActionType = "Open" Then
        Set fDialog = Application.FileDialog(msoFileDialogOpen)
    End If
    With fDialog
        .AllowMultiSelect = False
        ' Disallow user to make multiple selections in dialog box
        .Title = "Please specify the file to save/open..."
        ' Set the title of the dialog box.
        If ActionType <> "SaveAs" Then
            .Filters.Clear
            ' Clear out the current filters, and add our own.
            .Filters.Add FileType, "*." & FileType
        End If
        .InitialFileName = InitPath
        ' Show the dialog box. If the .Show method returns True, the
        ' user picked a 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
                'return the subroutine value as the file path & name selected
                Select_File = varFile
            Next
        End If
    End With
End Function
MarkII
  • 872
  • 1
  • 9
  • 26
1

I agree John M has best answer to OP's question. Thought not explictly stated, the apparent purpose is to get a selected file name, whereas other answers return either counts or lists. I would add, however, that the msofiledialogfilepicker might be a better option in this case. ie:

Dim f As object
Set f = Application.FileDialog(msoFileDialogFilePicker)
dim varfile as variant 
f.show
with f
    .allowmultiselect = false
     for each varfile in .selecteditems
        msgbox varfile
     next varfile
end with

Note: the value of varfile will remain the same since multiselect is false (only one item is ever selected). I used its value outside the loop with equal success. It's probably better practice to do it as John M did, however. Also, the folder picker can be used to get a selected folder. I always prefer late binding, but I think the object is native to the default access library, so it may not be necessary here

dcaswell
  • 3,137
  • 2
  • 26
  • 25
akw
  • 61
  • 1
  • 3