ACC2000: Error Using CurrentProject.Connection in Transactions (223213)
The information in this article applies to:
This article was previously published under Q223213 Advanced: Requires expert coding, interoperability, and multiuser skills.
This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).
SYMPTOMS
If you use the CommitTrans or RollbackTrans methods directly with the CurrentProject.Connection object, you may receive one of the following errors:
You tried to commit or rollback a transaction without first beginning a transaction.
No transaction is active.
CAUSE
Each time the CurrentProject.Connection object is used, it has a different pointer to the connection. In effect, the instance or pointer to that object is temporary. This is why you see no error on CurrentProject.Connection.BeginTrans, but you do receive an error once you try to commit or rollback the transaction.
RESOLUTIONMicrosoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.
Set an object variable once to CurrentProject.Connection and use it for your transaction processing, such as in the following example. Here, "c" is the Connection object used for transactions: NOTE: The sample code in this article uses Microsoft ActiveX Data Objects. For this code to run properly, you must reference the Microsoft ActiveX Data Objects 2.x Library (where 2.x is 2.1 or later.) To do so, click References on the Tools menu in the Visual Basic Editor, and make sure that the Microsoft ActiveX Data Objects 2.x Library check box is selected. -
Open the sample database Northwind.mdb.
- In the Database windows, click Modules under Objects, and then click New.
-
Type the following code in the new module:
Sub tstConnectionObj()
Dim c As ADODB.Connection
Dim rs As ADODB.Recordset
Set c = CurrentProject.Connection
Set rs = New ADODB.Recordset
rs.Open "Categories", c, adOpenDynamic, adLockOptimistic
c.BeginTrans
rs.Fields(1) = "Drinks"
rs.Update
MsgBox "Updated field to " & rs.Fields(1) & "."
c.RollbackTrans
rs.Requery
MsgBox "Rolled back field to " & rs.Fields(1) & "."
End Sub
- In the Immediate window, type the following and press ENTER:
tstConnectionObj
Note that you see the following message: - Click OK on the message. Note that you see the following message:
Rolled back field to Beverages.
- Click OK on the message.
Modification Type: | Major | Last Reviewed: | 6/23/2005 |
---|
Keywords: | kbprb KB223213 |
---|
|