VBA: 导入 xls 以访问允许用户指定要导入的 xls

VBA: Import xls to Access Allowing User to Specify xls to Import

使用 Access 2010...

我正在尝试 write/find 执行以下步骤的脚本:

  1. 运行 脚本
  2. 对话 window 打开(本质上是 Windows 资源管理器)允许用户导航到 select 用于导入的 xls
  3. 用户 selects xls
  4. Access 将此 xls 导入一个名为 Import
  5. 的 table
  6. 脚本结束

我找到了允许文件资源管理器 window 打开的片段:

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

但我不知道如何将它加入到类似的东西中:

    DoCmd.TransferSpreadsheet acImport, 10, "Import", "FILEPATH", True, ""

如有任何帮助,我将不胜感激。非常感谢。

FileDialog 是一个应用程序 object,包含一些组件,包括对话框标题 window、初始默认路径、用于指导用户正确类型的文件类型过滤器,其中一个重要项目是它的变体数组 .SelectedItems() 用于拉取所选文件的字符串。

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

f.AllowMultiSelect = False
f.Title = "Title of Dialog Window"
f.InitialFileName = "C:\Set\Default\Path"
f.Filters.Clear
f.Filters.Add "PDF files", "*.pdf"
f.FilterIndex = 1

If f.Show = -1 Then
    strFilePath = f.SelectedItems(1)
Else
   'The user pressed Cancel.
    MsgBox "No file Selected", vbExclamation
    strFilePath = Null        
End if

Set fd = Nothing    

从那里您可以将 strFilePath 传递到导入命令中:

DoCmd.TransferSpreadsheet acImport, 10, "Import", strFilePath, True, ""