The information in this article applies to:
- Microsoft Access versions 1.0, 1.1
SYMPTOMS
A subroutine or a function contains a transaction process which calls a
subroutine or function that dimensions and opens a database using the
OpenDatabase method. When the subroutine or function exits, Microsoft
Access implicitly closes the database (no Close method is called). When
the subroutine or function attempts to execute the CommitTrans or Rollback
statement, Microsoft Access displays the following message:
Commit or Rollback without BeginTrans.
CAUSE
When the called subroutine or function exits, Microsoft Access implicitly
closes the database. Internally, the initial attempt to close the database
fails because the database was opened in a transaction. Microsoft detects
this error and performs an implicit transaction rollback, then it closes
the database successfully.
RESOLUTION
To perform a Rollback in the called subroutine or function, you must first
call the Close statement to close the open database.
STATUS
This behavior no longer occurs in Microsoft Access version 2.0.
MORE INFORMATION
Steps to Reproduce Behavior
- Create the following module.
'************************************************************
'Declarations section of the module.
'************************************************************
Option Explicit
'============================================================
'The following Sub procedure starts a transaction, calls
'another sub procedure, then attempts to rollback.
'============================================================
Sub Test1
BeginTrans
Call Test2
Rollback
End Sub
'============================================================
'The following Sub procedure sets a Database variable to
'the current database.
'============================================================
Sub Test2
Dim MyDB as Database
Set MyDB = CurrentDB()
'the database is implicitly closed on exit
End Sub
- Compile the module.
- In the Immediate window, enter Test1. Microsoft Access generates
the following message:
Commit or Rollback without BeginTrans.
Perform the following four steps to demonstrate the resolution to this
problem.
- Perform Step 1 above.
- Edit the second function to add MyDB.Close, as follows.
'============================================================
'The following Sub procedure will set a Database variable to
'the current database, and then close the database.
'============================================================
Sub Test2
Dim MyDB as Database
Set MyDB = CurrentDB()
MyDB.Close
End Sub
- Compile the module.
- In the Immediate window enter Test1. The Rollback works correctly
because the code explicitly closes the database opened in the
transaction.