I have multiple SQL stored procedures (e.g. UPDATE, SELECT INTO statements) executed in VBA in MS Access:
我在MS Access中的VBA中执行了多个SQL存储过程(例如UPDATE,SELECT INTO语句):
CurrentDb.Execute "qry1"
CurrentDb.Execute "qry2"
CurrentDb.Execute“qry1”CurrentDb.Execute“qry2”
I want it so that:
* if qry2 fails, it will undo qry1.
* qry1 and qry2 are executed at the same time, (as I have many of these stored procedures executed in a chain), so the procedure runs faster.
我希望如此:*如果qry2失败,它将撤消qry1。 * qry1和qry2同时执行,(因为我在链中执行了许多这些存储过程),因此程序运行得更快。
How can this be done?
如何才能做到这一点?
1 个解决方案
#1
7
Transactions may suit, they allow rollback: http://msdn.microsoft.com/en-us/library/bb243155.aspx
事务可能适合,它们允许回滚:http://msdn.microsoft.com/en-us/library/bb243155.aspx
EDIT
编辑
Here is a rough example in DAO:
这是DAO中的一个粗略示例:
Dim strSQL As String
Dim db As DAO.Database
Dim wrk As Workspace
On Error GoTo TrapError
Set db = CurrentDb
Set wrk = DBEngine.Workspaces(0)
wrk.BeginTrans
strSQL = "Update sysInfo Set InvoiceOR=False"
db.Execute strSQL, dbFailOnError
wrk.CommitTrans
Exit_Sub:
Set db = Nothing
Set wrk = Nothing
Exit Sub
TrapError:
MsgBox "Failed: " & Err.Description
wrk.Rollback
Err.Clear
Resume Exit_Sub
Here are some rough notes for ADO:
以下是ADO的一些粗略说明:
Dim cmd As ADODB.Command
Dim cn As ADODB.Connection
Set cmd = CreateObject("ADODB.Command")
Set cn = CurrentProject.Connection
cmd.CommandText = "Update sysInfo Set InvoiceOR=False"
cmd.ActiveConnection = cn
cmd.ActiveConnection.BeginTrans
cmd.Execute , , adExecuteNoRecords
If Err <> 0 Then
cmd.ActiveConnection.RollbackTrans
Else
cmd.ActiveConnection.CommitTrans
End If
#1
7
Transactions may suit, they allow rollback: http://msdn.microsoft.com/en-us/library/bb243155.aspx
事务可能适合,它们允许回滚:http://msdn.microsoft.com/en-us/library/bb243155.aspx
EDIT
编辑
Here is a rough example in DAO:
这是DAO中的一个粗略示例:
Dim strSQL As String
Dim db As DAO.Database
Dim wrk As Workspace
On Error GoTo TrapError
Set db = CurrentDb
Set wrk = DBEngine.Workspaces(0)
wrk.BeginTrans
strSQL = "Update sysInfo Set InvoiceOR=False"
db.Execute strSQL, dbFailOnError
wrk.CommitTrans
Exit_Sub:
Set db = Nothing
Set wrk = Nothing
Exit Sub
TrapError:
MsgBox "Failed: " & Err.Description
wrk.Rollback
Err.Clear
Resume Exit_Sub
Here are some rough notes for ADO:
以下是ADO的一些粗略说明:
Dim cmd As ADODB.Command
Dim cn As ADODB.Connection
Set cmd = CreateObject("ADODB.Command")
Set cn = CurrentProject.Connection
cmd.CommandText = "Update sysInfo Set InvoiceOR=False"
cmd.ActiveConnection = cn
cmd.ActiveConnection.BeginTrans
cmd.Execute , , adExecuteNoRecords
If Err <> 0 Then
cmd.ActiveConnection.RollbackTrans
Else
cmd.ActiveConnection.CommitTrans
End If