gpt4 book ai didi

error-handling - MS Access回滚错误处理回滚

转载 作者:行者123 更新时间:2023-12-03 08:38:22 25 4
gpt4 key购买 nike

在我的程序中,我有一种功能,它可以完成三件事:在某些表中插入多个插入物,创建pdf文档,并发送带有附件中创建的文档的电子邮件。我在解决错误处理时遇到问题。

    DAO.DBEngine.BeginTrans
On Error GoTo failed

CurrentDb.Execute "INSERT INTO tblClaimStatus ( ClaimID, ClaimStatusTypeID, UserID) " & _
"VALUES (" & claimId & ", 2," & CurrentUserId & ");", dbFailOnError
CurrentDb.Execute "Insert INTO tblClaimPartner (ClaimId, OriginalDeadline, NachfristDeadline) " & _
"VALUES (" & claimId & ", '" & Me.OriginalDeadlineTextbox & "', '" & Me.NewDeadlineTextbox & "');", dbFailOnError

WriteGraceDocument (claimId)
SendEmail (claimId)
DAO.DBEngine.CommitTrans
问题是,如果发生任何故障,我希望能够回滚SQL事务。但是我还需要Subs WriteGraceDocuement和SendEmail中尚未提交的数据。
我该如何解决?

最佳答案

让函数返回成功或不成功:

DAO.DBEngine.BeginTrans

' Validate the values before attempting to insert these.
Success = ValidateInserts(claimId, 2, CurrentUserId, Me!OriginalDeadlineTextbox.Value, Me!NewDeadlineTextbox.Value)

If Success Then
CurrentDb.Execute "INSERT INTO tblClaimStatus ( ClaimID, ClaimStatusTypeID, UserID) " & _
"VALUES (" & claimId & ", 2," & CurrentUserId & ");", dbFailOnError
CurrentDb.Execute "Insert INTO tblClaimPartner (ClaimId, OriginalDeadline, NachfristDeadline) " & _
"VALUES (" & claimId & ", '" & Me.OriginalDeadlineTextbox & "', '" & Me.NewDeadlineTextbox & "');", dbFailOnError
End If

If Success Then
Success = WriteGraceDocument(claimId)
End If
If Success Then
Success = SendEmail (claimId)
End If

If Success Then
DAO.DBEngine.CommitTrans
Else
DAO.DBEngine.RollBack
End If

关于error-handling - MS Access回滚错误处理回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64549486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com