【问题标题】:VBA error 462 when updating table in Access from Excel从 Excel 更新 Access 中的表时出现 VBA 错误 462
【发布时间】:2019-01-10 09:55:13
【问题描述】:

从 Excel VBA 更新 Access 表时出现 462 运行时错误。我认为引用正确地使用了herehere 描述的对象变量进行了限定,但是在使用 DCount 将记录数分配给 dbImageCount 的行上,我仍然遇到错误。

运行时错误“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

在调试期间动态手动设置 dbImageCount 的值(注释掉 DCount 行)使用新图片数据正确更新数据库。

请务必注意,此问题并非始终如一地发生。经过几个月的使用,这个错误直到本周才出现,即便如此,每次更新尝试都没有发生。此外,它在开发过程中从未发生过(在不同的系统上)。

起初,我以为是网络故障或类似问题,但后来我读到 426 错误专门是 Office 自动化问题,所以我希望我们很快会再次看到它。

【问题讨论】:

    标签: excel vba ms-access automation dcount


    【解决方案1】:

    你需要使用DCount作为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
    

    【讨论】:

    • 好的,我会试试这个谢谢。您能否大胆猜测一下原始代码是如何在 DCount 配置不正确的情况下工作的?为什么 VBA 会在某些运行而不是其他运行中将 462 扔到那条线上……在某些计算机上而不是在其他计算机上?这类问题会让开发者发疯!
    • Afaik 它不应该工作。 DCount 和其他域聚合只能在从 Access 启动时用作函数。
    • 这很奇怪,因为当我开发它时,我从未见过 462 错误,而 Dcount 正在生成正确的值(表中具有相同 catalognum 和 lotnum 的记录数)。我会对其进行更严格的测试并与您联系。
    • 也许 MS 已更新 Office 以将 Access.Application 对象视为可启动 Dcount 等功能的活动 Access 会话。我刚刚对此进行了多次测试,在每种情况下,DCount 都产生了正确的记录数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 2014-04-15
    • 2020-03-18
    • 1970-01-01
    • 2019-03-29
    • 2021-04-07
    • 2017-02-21
    相关资源
    最近更新 更多