【发布时间】:2015-10-15 18:05:30
【问题描述】:
基本上,我在这里有一些代码可以从 Excel 更新 Access,它通过查看“监控 ID”是否等于“IngID”(本质上是监控 ID)来执行此操作。所以基本上如果它们匹配,代码需要更新访问表中标记为“TOWN”的字段。
但是,当我这样做时,.Fields("TOWN") = Cells(lngRow, 74).Value 出现错误。错误是:“运行时错误 3021 BOF 或 EOF 为 True,或者当前记录已被删除。请求的操作需要当前记录。”
该记录确实存在于表中,我已经尝试了多个,但它似乎仍然不起作用,有什么想法吗?
Application.ScreenUpdating = False ' Prevents screen refreshing.
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim fld As ADODB.Field
Dim MyConn
Dim lngRow As Long
Dim lngID, LR, Upd
Dim sSQL As String
LR = Range("BN" & Rows.Count).End(xlUp).Row
Upd = LR - 1
lngRow = 1
Do While lngRow <= LR
lngID = Cells(lngRow, 66).Value
sSQL = "SELECT * FROM Tbl_Primary WHERE MonitorID = '" & lngID & "'"
Set cnn = New ADODB.Connection
MyConn = "Provider = Microsoft.ACE.OLEDB.12.0;" & _
"Data Source =location of access file.mdb"
With cnn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open MyConn
End With
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.Open sSQL, ActiveConnection:=cnn, _
CursorType:=adOpenKeyset, LockType:=adLockOptimistic
'Load all records from Excel to Access.
With rst
'
.Fields("TOWN") = Cells(lngRow, 74).Value
rst.Update
End With
' Close the connection
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
lngRow = lngRow + 1
Loop
MsgBox "Update Complete"
【问题讨论】:
标签: excel ms-access vba sql-update