如何在Access 2007 VBA中显示“打开文件”对话框?

时间:2009-07-07 10:12:55

标签: ms-access access-vba

如何在Access 2007 VBA中显示打开的文件(或文件选择)对话框?

我尝试过在Excel中使用Application.GetOpenFileName,但Access中不存在此功能。

5 个答案:

答案 0 :(得分:44)

我对Renaud Bompuis答案的评论搞砸了。

实际上,您可以使用后期绑定,并且不需要引用11.0对象库。

以下代码无需任何参考即可使用:

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

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

请注意,上述内容在运行时也很有用。

答案 1 :(得分:19)

在Access 2007中,您只需使用Application.FileDialog

以下是Access文档中的示例:

' 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

如示例所示,只需确保您引用 Microsoft Access 12.0对象库(在VBE IDE> Tools> References菜单下)。

答案 2 :(得分:3)

除了艾伯特已经说过的话:

此代码(各种样本的混搭)提供了具有SaveAs对话框的功能

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

答案 3 :(得分:1)

我有类似的解决方案,它适用于打开,保存,文件选择。我将其粘贴到自己的模块中,并在我创建的所有Access数据库中使用。正如代码所述,它需要Microsoft Office 14.0对象库。我想是另一种选择:

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

答案 4 :(得分:0)

我同意John M对OP的问题有最好的答案。如果没有明确说明,明显的目的是获得一个选定的文件名,而其他答案则返回计数或列表。但是,我想补充一点,在这种情况下,msofiledialogfilepicker可能是更好的选择。即:

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

注意:varfile的值将保持不变,因为multiselect为false(只选择了一个项目)。我在循环外使用它的值同样成功。然而,这可能是John M所做的更好的做法。此外,文件夹选择器可用于获取选定的文件夹。我总是喜欢后期绑定,但我认为该对象是默认访问库的原生对象,所以这里可能没有必要