简体   繁体   English

从 Excel 更新 Access 中的表时出现 VBA 错误 462

[英]VBA error 462 when updating table in Access from Excel

I'm getting the 462 runtime error when updating an Access table from Excel VBA.从 Excel VBA 更新 Access 表时,我收到 462 运行时错误。 I think the references are correctly qualified with the object variable as described here and here , but I'm still getting an error on the line where the number of records is assigned to dbImageCount using DCount.我认为引用正确地使用对象变量进行了限定,如此此处所述,但在使用 DCount 将记录数分配给 dbImageCount 的行上,我仍然遇到错误。

Run-Time error '462': The remote server machine does not exist or is unavailable运行时错误“462”:远程服务器计算机不存在或不可用

Public AppAccess As Access.Application
...
Sub btnSave2Access_Click()
    Dim MyRow As Long, LastCaptionRow As Integer
    Dim sPath As String, STblName As String, CatalogNum As String, LotNum As String
    Dim i As Integer, dbImageCount As Integer
    CatalogNum = Trim(Sheets("Tier2Worksheet").Range("B2"))
    LotNum = Trim(Sheets("Tier2Worksheet").Range("B3"))
    LastCaptionRow = Range("E1000").End(xlUp).Row
    sPath = Sheets("Settings").Range("B16")
    STblName = "tblProductPictures"
    Set AppAccess = New Access.Application
    With AppAccess
        .OpenCurrentDatabase sPath
        For i = 1 To LastCaptionRow
            'error in next line
            dbImageCount = DCount("[SortOrder]", STblName, "[CatalogNum] = '" & CatalogNum & "' AND [LotNum] = '" & LotNum & "'") 'get current image count in DB for catNum/LotNum combo
            While dbImageCount < LastCaptionRow 'adds record to picture table when required
                dbImageCount = dbImageCount + 1
                .DoCmd.RunSQL "INSERT INTO " & STblName & " (CatalogNum, LotNum, SortOrder) VALUES ('" & CatalogNum & "','" & LotNum & "','" & dbImageCount & "');"
                DoEvents
            Wend
            With .DoCmd
                .SetWarnings False
                .RunSQL "UPDATE " & STblName & " SET PicPath='" & Range("E" & i) & "' Where [CatalogNum]='" & CatalogNum & "' and [SortOrder]='" & i & "' and [LotNum]='" & LotNum & "';"
                .RunSQL "UPDATE " & STblName & " SET FullCaption='" & Range("D" & i) & "' Where [CatalogNum]='" & CatalogNum & "' and [SortOrder]='" & i & "' and [LotNum]='" & LotNum & "';"
                .SetWarnings True
            End With
        Next i
        .CloseCurrentDatabase
        .Quit
    End With
    Set AppAccess = Nothing
    Application.StatusBar = False
End Sub

Manually setting the value of dbImageCount on the fly during debug (commenting out the DCount line) properly updates the database with the new picture data.在调试期间动态手动设置 dbImageCount 的值(注释掉 DCount 行)使用新图片数据正确更新数据库。

It's important to note that this problem does not occur consistently.需要注意的是,此问题不会持续发生。 After months of use, the error did not creep up until this week and even then it didn't happen for every update attempt.经过数月的使用,直到本周该错误才出现,即使如此,每次更新尝试也不会发生。 In addition, it never happened during development (on a different system).此外,它从未发生在开发过程中(在不同的系统上)。

At first, I thought it was a network glitch or something of the like, but then I read that the 426 error is specifically an Office automation problem, so I expect that we will see it again soon.起初,我以为是网络故障或类似问题,但后来我读到 426 错误具体是 Office 自动化问题,所以我希望我们很快就会再次看到它。

You need to use DCount as a method of the Access Application:您需要使用DCount作为 Access 应用程序的方法:

With AppAccess
    .OpenCurrentDatabase sPath
    For i = 1 To LastCaptionRow
        'error in next line
        dbImageCount = .DCount("[SortOrder]", STblName, "[CatalogNum] = '" & CatalogNum & "' AND [LotNum] = '" & LotNum & "'") 'get current image count in DB for catNum/LotNum combo
        While dbImageCount < LastCaptionRow 'adds record to picture table when required
            dbImageCount = dbImageCount + 1
            .DoCmd.RunSQL "INSERT INTO " & STblName & " (CatalogNum, LotNum, SortOrder) VALUES ('" & CatalogNum & "','" & LotNum & "','" & dbImageCount & "');"
            DoEvents
        Wend
        With .DoCmd
            .SetWarnings False
            .RunSQL "UPDATE " & STblName & " SET PicPath='" & Range("E" & i) & "' Where [CatalogNum]='" & CatalogNum & "' and [SortOrder]='" & i & "' and [LotNum]='" & LotNum & "';"
            .RunSQL "UPDATE " & STblName & " SET FullCaption='" & Range("D" & i) & "' Where [CatalogNum]='" & CatalogNum & "' and [SortOrder]='" & i & "' and [LotNum]='" & LotNum & "';"
            .SetWarnings True
        End With
    Next i
    .CloseCurrentDatabase
    .Quit
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM