我知道了如何在查询出错时运行事务。然而,我试图在事务中包含的追加查询是从空白表单中提取值,据我所知,这意味着我需要在代码中定义查询参数。
这是事务和错误处理工作的代码:
Private Sub Command0_Click()
Dim ws As DAO.Workspace, db As DAO.Database
Set ws = DBEngine.Workspaces(0)
Set db = ws.Databases(0)
Set qdf = db.QueryDefs
On Error GoTo ErrTrap
ws.BeginTrans
DoCmd.SetWarnings False
db.Execute "TESTQRY1", dbFailOnError
db.Execute "TEST2QRY", dbFailOnError
db.Execute "TESTQRY3", dbFailOnError
ws.CommitTrans
MsgBox ("You have successfully updated the data")
DoCmd.SetWarnings True
Exit Sub
ErrTrap:
ws.Rollback
MsgBox "Rollback needed because:" & vbCr & Err.Description
End Sub
这是用于实际运行查询的代码,但没有错误处理或事务:
Private Sub Add_Click()
Dim db As DAO.Database
Dim qry As DAO.QueryDef
Dim ws As DAO.Workspace
Set ws = DBEngine.Workspaces(0)
Set db = ws.Databases(0)
Set qry = db.QueryDefs("APPENDQRY")
qry.Parameters(0) = Forms!LotNumberFrm!txtLotNumber
qry.Parameters(1) = Forms!LotNumberFrm!txtFWNumber
qry.Parameters(2) = Forms!LotNumberFrm!txtExpDate
qry.Parameters(3) = Forms!LotNumberFrm!chkActive
qry.Execute
Exit Sub
End Sub
所以基本上我的问题是,我需要同时做这两件事——将查询作为带有错误处理的事务的一部分运行,并在代码中定义查询参数。