使用事务(excel)进行DAO Recordset批量更新

时间:2018-07-30 09:35:01

标签: excel vba transactions dao recordset

我正在尝试将使用VBA从Excel窗体中开始的DAO事务中的批量记录导出到Access表,但是当我在下面的代码中单击commit时,什么都没有导出,它一定很接近,但是我不明白为什么我的交易是空的?如果将ws.committrans移到与启动事务相同的按钮单击事件,则一切正常,但是那是一次更新,我想使用事务一次更新多个记录。

Public db As DAO.Database
Public rs As DAO.Recordset
Public ws As DAO.Workspace

Public Sub CommandButton1_Click()
Dim val1 As String
Dim val2 As String
Dim val3 As String
Dim val4 As String
Dim val5 As String
Dim val6 As String

Set ws = DBEngine(0)
Set db = ws.OpenDatabase("Database8")
Set rs = db.OpenRecordset("test")



val1 = Me.TextBox1.Value
val2 = Me.TextBox2.Value
val3 = Me.TextBox3.Value
val4 = Me.TextBox4.Value
val5 = Me.TextBox5.Value
val6 = Me.TextBox6.Value

ws.BeginTrans

With rs
.AddNew
!col1 = val1
!col2 = val2
!col3 = val3
!col4 = val4
!col5 = val5
!col6 = val6
.Update
End With


Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""
Me.TextBox5.Value = ""
Me.TextBox6.Value = ""




End Sub

Private Sub CommandButton2_Click()

ws.CommitTrans

End Sub

1 个答案:

答案 0 :(得分:0)

更改代码以在激活用户表单时启动事务,并在单击提交按钮时完成交易,现在就像梦一样。