更新记录集而不更新数据库

时间:2015-06-02 10:09:22

标签: excel vba recordset

我使用ADODB连接连接到Excel VBA中的Access数据库,并获取带有记录集的记录。我想更新记录集,但是当我更新记录集时,我也会更新数据库中的表。是否可以更新记录集而不是数据库?这是我的代码,

Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset

con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\jasons\Documents\UPD.accdb"
rs.Open "SELECT ITEM, SL AS SL FROM Table2", con, adOpenDynamic, adLockPessimistic

rs.MoveFirst
  Do
        rs.Update "SL", 250
        rs.MoveNext
    Loop Until rs.EOF

con.Close
Set con = Nothing

1 个答案:

答案 0 :(得分:2)

感谢pony2deer的链接,我只通过添加

调整了我的代码
rs.CursorLocation = adUseClient
rs.LockType = adLockBatchOptimistic

这是完整的代码,

Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
sql = "SELECT * FROM Table2"

con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\jasons\Documents\UPD.accdb"
rs.CursorLocation = adUseClient
rs.LockType = adLockBatchOptimistic
rs.Open sql, con

Set rs.ActiveConnection = Nothing
con.Close

rs.MoveFirst
  Do
        rs.Update "SL", 20
        rs.MoveNext
    Loop Until rs.EOF