I am trying to set up a transaction within Access itself using VBA. So far I have tried the following:
This isn't my code, the code is in a real function. These are just the commands that I put around my SQL code to try this.
I am using DoCmd.RunSql "<the sql>" to run the INSERT and DELETE queries.
So, does anyone know how to do this the right way?
This is my real code:
Code:
' This line doesn't error, but will error on CurrentProject.Connection.Rollback / .CommitTrans
' saying no transaction is started
CurrentProject.Connection.BeginTrans
' These lines do not error but do not do anything!
DBEngine.BeginTrans
DBEngine.Rollback
DBEngine.CommitTrans
' and
DBEngine.Workspaces(0).BeginTrans
DBEngine.Workspaces(0).Rollback
DBEngine.Workspaces(0).CommitTrans
I am using DoCmd.RunSql "<the sql>" to run the INSERT and DELETE queries.
So, does anyone know how to do this the right way?
This is my real code:
Code:
Private Function BackupData() As Boolean
On Error GoTo ErrorTrap
Dim X As Integer
Dim Y As Integer
Dim sPKList As String
Dim rPKList As ADODB.Recordset
Dim SQL As String
BackupData = False
DBEngine.Workspaces(0).BeginTrans
DoCmd.RunSQL "INSERT INTO ARCHIVE_MailMaster SELECT * FROM MailMaster WHERE Study = '" & aryStudyList(X) & "';"
DoEvents
DoCmd.RunSQL "DELETE * FROM MailMaster WHERE Study = '" & aryStudyList(X) & "';"
DoEvents
' Force an error to test Rollback
Debug.Print 1/0
' lots more code here to INSERT/DELETE from other tables
DBEngine.Workspaces(0).CommitTrans
ExitPoint:
Exit Function
ErrorTrap:
DBEngine.Workspaces(0).Rollback
BackupData = False
Resume ExitPoint
End Function