Microsoft KB Archive/223213

= ACC2000: Error Using CurrentProject.Connection in Transactions =

Article ID: 223213

Article Last Modified on 6/23/2005

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



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.

-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. 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:

Updated field to Drinks.

 Click OK on the message. Note that you see the following message:

Rolled back field to Beverages.

 Click OK on the message.</li></ol>

<div class="moreinformation_section">

Steps to Reproduce Behavior
<ol> Open the sample database Northwind.mdb.</li> In the Database windows, click Modules under Objects, and then click New.</li>  Type the following code in the new module: 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 </li>  In the Immediate window, type the following and press ENTER: tstCurrentProj Note that you see the following message:

Updated field to Drinks.

</li> Click OK on the message.

Note that you see the first error message mentioned in the "Symptoms" section of this article.</li></ol>

Additional query words: prb

Keywords: kbprb KB223213

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.