MS Access 2010: adding transaction management to a form
MS-Access 2010 environment looks unstable after using transactions (BeginTrans / CommitTrans / Rollback). By instability, I mean that MS-Access no longer allows any modifications to objects (it automatically turns into some kind of exclusive mode) or shows unexpected error messages when executing any SQL queries of the process.
Situation :
I have MS-Access 2010 database (format accdb
) where I added a new VBA module. This module processes this file to load its records into a table in the database. I am using MS-Access 2010 environment to work with this DB (not a standalone application)
For each record, some checks are done before they are saved to db. If any error is found, all processing is canceled. I use transactions to ensure the complete file is up and running.
Problem
-
If the first time I start this process, it is correct and therefore it ends with a COMMIT, the process can be re-started with other files without significantly processing them. The result of each processing depends only on the quality of the data in the file.
-
But if the first time I start this process, it ends up with ROLLBACK, each next start of the process ends up with errors when executing any SQL query in this process.
In both cases, the MS-Access environment becomes a kind of exclusive mode.
Questions :
Is this VBA module or processing compatible with MS-Access 2010 IDE? or I have to create a new standalone application that connects to my Access database to start my file processing.
Code (simplified) :
Private Sub Comando0_Click() 'A success processing simulation
On Error GoTo ErrManagenent
BeginTrans
'Some procesing finishin OK
ProcessWithoutError
CommitTrans
Exit Sub
ErrManagenent:
Debug.Print "Comando0_Click Error: " & Err.Description & "." & vbCrLf & Err.Source
Rollback
End Sub
Private Sub Comando6_Click() 'A fail processing simulation.
On Error GoTo ErrManagenent
BeginTrans
'Some procesing finishin with a Err.Raise
ProcessWithError
Exit Sub
ErrManagenent:
Debug.Print "Comando6_Click Error: " & Err.Description & "." & vbCrLf & Err.Source
Rollback
End Sub
Private Sub ProcessWithError() 'simulation of a process ending with error
Dim rs As Recordset
Dim strSql As String
'Any DB query.
strSql = "SELECT * FROM 00_Bancos"
Set rs = CurrentDb.OpenRecordset(strSql)
Debug.Print rs.RecordCount
'some processing with
'......
'let suppose there is an error while processing
rs.Close
Err.Raise 11, , "MY error mesage"
End Sub
Private Sub ProcessWithoutError() 'Simulation of a process ending OK
Dim rs As Recordset
Dim strSql As String
'Any DB query.
strSql = "SELECT * FROM 00_Bancos"
Set rs = CurrentDb.OpenRecordset(strSql)
Debug.Print rs.RecordCount
'some processing
'......
'let suppose the process finishes OK.
rs.Close
End Sub
source to share
you should only use transactions if you are inserting / updating multiple SQL statements which then make sense to rollback. Also use transactions just before SQL execution and trap errors to figure out which SQL statement is causing the failure.
pseudo:
- Make a check
- Prepare SQL statements
- Prepare SQL Statement 2
- Transaction start
- Executing SQL statements
- Undo or rollback
in code it would be:
Private Sub mTrans()
Dim myDB As DAO.Database
Set myDB = CurrentDb
Dim SQL_SET As String
SQL_SET = "First sql statement"
On Error GoTo ERROR_SQL1:
DBEngine.BeginTrans
myDB.Execute SQL_SET, dbFailOnError
On Error GoTo ERROR_SQL2:
SQL_SET = "second sql statement..." 'either use the same variable or use SQL_SET1 for better overview
myDB.Execute SQL_SET, dbFailOnError
DBEngine.CommitTrans
EXIT_SUB:
On Error Resume Next
Set myDB = Nothing
Exit Sub
ERROR_SQL1:
DBEngine.Rollback
MsgBox "Error while executing sql_1. " & vbNewLine & "System msg: " & Err.description
GoTo EXIT_SUB
ERROR_SQL2:
DBEngine.Rollback
MsgBox "Error while executing sql_2. " & vbNewLine & "System msg: " & Err.description
GoTo EXIT_SUB
End Sub
source to share