【发布时间】:2021-03-13 08:57:10
【问题描述】:
我有一个 Excel VBA 来更新 Access 中的数据。我使用下面的脚本。 我只是有一个小问题。如果我用 rs.close 关闭记录集,我会收到消息“对象关闭时不允许操作”。如果我不使用 rs.close ,那么它可以完美运行。
-
使用 sql 时真的需要 rs.close 吗?
-
我也试过用cn.execute updatesql。如何设置记录锁定属性?
Public Sub Upload_to_DB() Call setparameters ' exports data from the worksheet to a table in an Access database Dim cn As ADODB.Connection ws As Worksheet , updatesql As String, r_fld As Long, r_val As Long, c As Long, str As String, rs As ADODB.Recordset Set cn = CreateObject("ADODB.Connection") cn.mode = 16 + 3 cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _ "Data Source= " & Path & filename & ";" cn.CursorLocation = adUseClient Set rs = CreateObject("ADODB.Recordset") Set ws = Sheets("Opportunity_down_and_upload") With ws r_fld = 13 r_val = 14 c = 3 str = Empty str = .Cells(r_fld, c).Offset(0, -1).Value & " = " & .Cells(r_val, c).Offset(0, -1).Value continue = True Do str = str & " , " & .Cells(r_fld, c).Value & " = " & .Cells(r_val, c).Value c = c + 1 If IsEmpty(.Cells(r_fld, c)) Then continue = False Loop Until continue = False End With 'This part is important:' updatesql = "UPDATE tbl_D_opp_prod_offer SET " updatesql = updatesql & str updatesql = updatesql & " WHERE [Opp_ID] = " & ws.Range("A10") & ";" rs.Open updatesql, cn, , adLockOptimistic, adCmdText cn.Close Set rs = Nothing Set cn = Nothing End Sub
【问题讨论】:
标签: vba ms-access-2010 adodb