【问题标题】:I am getting an unknown file path error and I can't figure out why?我收到一个未知的文件路径错误,我不知道为什么?
【发布时间】:2020-08-13 13:16:07
【问题描述】:

我在 Access 中有一个 VBA 代码部分,它使用表单上的输入运行查询(一个输入用于查询,另一个用于保存文件名)。出于某种原因,我收到了无效的文件路径错误,但据我所知,路径是合法的。为什么我收到错误消息? FileName 是要从表单中保存的电子表格的所需名称。 这是我从访问错误中得到的:

Private Sub AllPaybacks_Click()
Dim getFolder As Object
Dim sLoc As String
Dim fileN As String

Set getFolder = Application.FileDialog(msoFileDialogFolderPicker)
With getFolder
    .AllowMultiSelect = False
    getFolder.InitialFileName = FileName.Value
    fileN = getFolder.InitialFileName
    If .Show = True Then
        sLoc = getFolder.SelectedItems(1) & "\"
    End If
End With

DoCmd.OpenQuery "PaybackQ"
DoCmd.TransferSpreadsheet acExport, , "PaybackQ", sLoc & fileN & ".xlsx", True
End Sub

【问题讨论】:

  • 您是否尝试过其他文件路径?文件是否被锁定或您有权限?
  • @nicomp 我尝试了其他路径和文件名。当我在 VBA 代码中硬编码文件名时,它工作正常。当我添加 getFolder.InitialFileName = FileName.Value fileN = getFolder.InitialFileName 以尝试从表单上的文本框中提取所需的文件名时出现此问题。
  • TransferSpreadsheet 语句中是否发生这种情况?
  • 您是否对从错误消息中复制的路径进行硬编码并且有效?
  • @nicomp 是的,错误发生在 transferspreadsheet 命令中。此外,当我在 if 语句之前将 fileN 设置为诸如“test”之类的字符串时,它工作正常。我也尝试对路径进行硬编码并得到同样的错误。似乎错误在于 getFolder.InitialFileName = FileName.Value fileN = getFolder.InitialFileName 这行我的目标是获取表单文本框中的文本并将其用作文件名。

标签: vba ms-access ms-access-2013


【解决方案1】:

我使用错误的语法来解决这个问题。为了将表单文本框中的数据用作文件名,我需要执行以下操作。

Private Sub AllPaybacks_Click()
Dim getFolder As Object
Dim sLoc As String
Dim fileN As String

Set getFolder = Application.FileDialog(msoFileDialogFolderPicker)
With getFolder
    .AllowMultiSelect = False
    fileN = Forms!PaybackSearchF!FileName
    If .Show = True Then
        sLoc = getFolder.SelectedItems(1) & "\"
    End If
End With

DoCmd.OpenQuery "PaybackQ"
DoCmd.TransferSpreadsheet acExport, , "PaybackQ", sLoc & fileN & ".xlsx", True

End Sub

fileN = Forms!PaybackSearchF!FileName 从我的表单中正确读取文本框中的字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-23
    • 2019-02-14
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多