【问题标题】:Intermittent Access error 3420 "Object invalid or no longer set."间歇访问错误 3420“对象无效或不再设置。”
【发布时间】:2016-03-18 08:50:41
【问题描述】:

任何人都可以告诉我我的代码的问题在哪里它的工作时间从 10 次到 3 次,问题在哪里 procces 导入一个 Excel 文件并在插入新行并编辑行中和 Inputbox 之后的名称后删除第一行和第二行,但我认为问题就在这里

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "ps", imyDateiname, 真

Private Sub Command50_Click()

 DoCmd.SetWarnings False
    Const msoFileDialogFilePicker As Long = 1
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    Dim selectedFilter As String
    Dim imyDateiname As String
    With fDialog
        .AllowMultiSelect = False
        .Filters.Clear
                .Filters.Add "Excel", "*.xlsx", 1
        .Show
        If .SelectedItems.Count = 0 Then
            imyDateiname = ""
        Else
            imyDateiname = Dir(.SelectedItems(1))
        End If
        selectedFilter = .FilterIndex
    End With
    If imyDateiname <> "" Then
        Dim oExc As New Excel.Application

        With oExc    
            .Workbooks.Open "C:\Users\" & Environ("UserName") _ 
                                 & "\Desktop\" & imyDateiname

            .Rows("1:2").Delete
            .Rows("1:2").EntireRow.Insert
            .Worksheets("ps").Columns("B").Replace _
                   What:="-", Replacement:=" ", _
                   SearchOrder:=xlByColumns, MatchCase:=True
        End With

        With oExc
              Cells(1, 1).Value2 = "Ebene"
              Cells(1, 2).Value2 = "OrgEinheit"
              Cells(1, 3).Value2 = "Titel"
              Cells(1, 4).Value2 = "PersNr"
              Cells(1, 5).Value2 = "Geburtsdatum"
              Cells(1, 6).Value2 = "Eintrittsdatum"
              Cells(1, 7).Value2 = "Befristungs"
              Cells(1, 8).Value2 = "Beginnalter"
              Cells(1, 9).Value2 = "Beginnfrei"
              Cells(1, 10).Value2 = "WK2"
              Cells(1, 11).Value2 = "WT"
              Cells(1, 12).Value2 = "Kostenstelle"
              Cells(1, 13).Value2 = "Schlüssel"
              Cells(1, 14).Value2 = "Tätigkeitsbezeichnung"
              Cells(1, 15).Value2 = "IRWAZ"
              Cells(1, 16).Value2 = "IstAK"
              Cells(1, 17).Value2 = "BelGrp"

            .ActiveWorkbook.SaveAs "C:\Users\" & Environ("UserName") _
                      & "\Desktop\" & imyDateiname
            .Quit
        End With   

        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
              "ps", imyDateiname, True

        Dim str As String
        str = InputBox("INSERT THE NUMBER OF THE MONTH PLEASE")
        execQry "upd_TPS_Monat", str
        Me.Refresh
        MsgBox "DONE YOUR DATA ARE READY!"
   End If
End Sub**

【问题讨论】:

  • re: “我认为问题就在这里” - 我们需要知道的第一件事就是哪条语句失败了。出现错误后点击“Debug”,DoCmd.TransferSpreadsheet语句是否高亮?
  • 是的,当我单击“调试”时,它会在此处显示黄线
  • DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "ps", imyDateiname, True
  • 再看代码,你的变量imyDateiname只包含文件名,没有路径。因此,您的 TransferSpreadsheet 依赖于 Access,其“当前目录”位于存储文件的路径中。这也可能导致问题。您应该提供完整的文件路径。
  • ist 现在工作得更好,但我仍然有新的错误 462,远程服务器机器不存在或不可用,我也收到错误 3274 外部表不在预期格式

标签: ms-access vba ms-office


【解决方案1】:

您应该首先关闭/退出 Excel 对象,然后将其设置为空

Set oExc = Nothing

请参阅this Post by Bob Larson

Excel 对象可能仍保留文件,将对象设置为 Nothing 可增强垃圾收集,女巫放开文件,并允许 Access 导入其内容。 编辑:请参阅this reference about Garbage collecting in VBA

如果不出意外,这行代码将添加一些纳秒,并让 import 运行没有错误。

【讨论】:

  • 一些建议:如果您只想在 Access 中管理字段名称,请使用 HasFieldNames Parameter = False 导入数据,然后使用 'TableDefs("ps").Fileds (FiledOrdinalNumber).Name = strFiledName',用于管理 Access 中的字段名称。 FiledOrdinalNumber 应该是代码中的 Coilum 编号。
  • 这应该是Set oExc = Nothing。第一个链接出了点问题。
  • 谢谢,安德烈,我确定了答案。
  • ist 现在工作得更好,但我有新的错误 462,远程服务器机器不存在或不可用,我也收到错误 3274 外部表不是预期的格式
  • 至于错误 462,嗯,这就是为什么我倾向于避免使用 Excel 对象...您如何看待我在前面评论中的建议?如果你想在 Access 中管理字段名称,为什么不从 access 中管理它们呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-06
  • 2019-03-30
  • 1970-01-01
  • 1970-01-01
  • 2013-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多