从 Excel VBA 更新 Access 表时出现 462 运行时错误。我认为引用正确地使用了此处和此处所述的对象变量进行了限定,但是在使用 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 自动化问题,所以我希望我们很快会再次看到它。