【问题标题】:TransferSpreadsheet acImport defaulting to documents folder - Run-time error 3011 (Access VBA)TransferSpreadsheet acImport 默认为文档文件夹 - 运行时错误 3011(Access VBA)
【发布时间】:2021-05-13 20:37:30
【问题描述】:

目前使用 DoCmd.TransferSpreadsheet acImport 对我不起作用。我正在尝试导入 .xlsx 文件,并使用通配符 (*) 来完成文件名。当我使用 MsgBox 时,FullPath 是正确的,但是当 TransferSpreadsheet 运行时,它说它在 C:\Users\Me\Documents (默认位置)中找不到文件。

Dim FPath As String
Dim FName As String
Dim FullPath As String
FPath = CurrentProject.Path & "\Data\"
FName = "DataTable"
FullPath = Dir(FPath & FName & "*.xlsx")
If FullPath <> "" Then
     DoCmd.TransferSpreadsheet acImport, 10, TableName, FullPath, 1
     Else: MsgBox "Error - file not found"
End If

为什么它不在我指定的地方寻找?这个错误是不是不正确,它表明了其他东西?

【问题讨论】:

  • 你没有为FilePath指定任何东西。
  • 不能在要导出到的文件名中使用通配符。您在模块标题中有 Option Explicit 吗?您将 FullPath 调暗,但使用 FilePath 导出文件名。
  • 道歉 - 我已经更正了。
  • @June7 我刚刚在 FullPath 的定义周围添加了一个 Dir() 来解决这个问题,但它仍然不起作用。这是否指向与您建议的问题不同的问题?
  • 嗯,FullPath 值有一个通配符。这不适用于指定要保存到的文件名。为什么还要使用通配符?

标签: vba ms-access


【解决方案1】:

Dir() 只返回一个文件 name 或什么都不返回(空字符串)。它不返回完整路径。

Dim FPath As String
Dim FileName As String

FPath = CurrentProject.Path & "\Data\"

FileName = Dir(FPath & "DataTable*.xlsx")

If FileName <> "" Then
    DoCmd.TransferSpreadsheet acImport, 10, TableName, FPath & FileName, 1
Else
    MsgBox "Error - file not found"
End If

【讨论】:

  • 这似乎不正确 - 当我将 MsgBox FileName 添加到我的代码中时,它会返回完整的文件路径。
  • 这不是记录在案的行为,也不是我所看到的。例如,即时窗格中的? Dir("C:\Tester\*.xlsx") 给我Data.xlsx 而不是C:\Tester\Data.xlsx
  • 我的行为与@TimWilliams 描述的相同。
  • 我的错误,我引用了我的代码的错误部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多