首页 > 解决方案 > TransferSpreadsheet acImport 默认为文档文件夹 - 运行时错误 3011 (Access VBA)

问题描述

使用 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

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

标签: vbams-access

解决方案


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

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

推荐阅读