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