Update recordset without updating database

◇◆丶佛笑我妖孽 提交于 2020-01-15 03:24:31

问题


I use an ADODB connection to connect to my Access DB in Excel VBA and get the records with a recordset. I want to update the recordset, but when i update the recordset i also update the table in the DB. Is it possible to update the recordset but not the db? Here is my code,

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:


Thanks to the link by pony2deer i adjusted my code only by adding

rs.CursorLocation = adUseClient
rs.LockType = adLockBatchOptimistic

Here is the full code,

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


来源:https://stackoverflow.com/questions/30593121/update-recordset-without-updating-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!