ACC2000: Error Using CurrentProject.Connection in Transactions

ID: Q223213


The information in this article applies to:

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and 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.
-or-
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.


RESOLUTION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
CAUTION: Following the steps in this example will modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and perform 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 need to reference the Microsoft ActiveX Data Objects 2.1 Library.

  1. Open the sample database Northwind.mdb.


  2. In the Database windows, click Modules under Objects, and then click New.


  3. Type the following code in the new module:


  4. 
    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 
  5. In the Immediate window, type the following and press ENTER:
    
    tstConnectionObj 
    Note that you see the following message:


  6. Updated field to Drinks.
  7. Click OK on the message. Note that you see the following message:


  8. Rolled back field to Beverages.
  9. Click OK on the message.



MORE INFORMATION

Steps to Reproduce Behavior

  1. Open the sample database Northwind.mdb.


  2. In the Database windows, click Modules under Objects, and then click New.


  3. Type the following code in the new module:


  4. 
    Sub tstCurrentProj()
    
       Dim rs As ADODB.Recordset
    
       Set rs = New ADODB.Recordset
    
       rs.Open "Categories", CurrentProject.Connection, adOpenDynamic, _
         adLockOptimistic
    
       CurrentProject.Connection.BeginTrans
       rs.Fields(1) = "Drinks"
       rs.Update
       MsgBox "Updated field to " & rs.Fields(1) & "."
    
       CurrentProject.Connection.RollbackTrans
       MsgBox "Rolled back field to " & rs.Fields(1) & ".
    
    End Sub 
  5. In the Immediate window, type the following and press ENTER:
    
    tstCurrentProj 
    Note that you see the following message:


  6. Updated field to Drinks.
  7. Click OK on the message.

    Note that you see the first error message mentioned in the "Symptoms" section of this article.


Additional query words: prb


Keywords          : 
Version           : WINDOWS:2000
Platform          : WINDOWS 
Issue type        : kbprb 

Last Reviewed: July 6, 1999