Article ID: 131342
Article Last Modified on 1/19/2007
Option Explicit
Function Trans_Work ()
Dim db As Database
Dim ws As WorkSpace
Dim rs As Recordset
Dim connectstring As String
connectstring = "ODBC;DSN=<datasource name>;UID=<username>;_
PWD=<password>;DATABASE=PUBS"
' NOTE: In the "connectstring" line above replace <datasource
' name> with the name of your data source for SQL Server; replace
' <username> with the username used to log on to the data
' source; and replace <password> with the appropriate password.
On Error GoTo Trans_Work_Err
Set ws = dbengine.Workspaces(0)
Set db = ws.OpenDatabase("", False, False, connectstring)
Set rs = db.OpenRecordset("dbo.authors") 'Opens the recordset.
ws.BeginTrans 'Starts the transaction.
rs.MoveLast
Debug.Print rs![au_lname]
ws.CommitTrans 'Commits the transaction.
rs.Close 'Closes the Recordset.
db.Close
Exit Function
Trans_Work_Err:
ws.Rollback
If Err = 3146 Then 'ODBC call failed
Error (Err)
Else
MsgBox Error$ 'The message if a different error occurs.
End If
Exit Function
End Function
? Trans_Work()
Option Explicit
Function Trans_Fail ()
Dim db As Database
Dim ws As WorkSpace
Dim rs As Recordset
Dim connectstring As String
connectstring = "ODBC;DSN=<datasource name>;UID=<username>;_
PWD=<password>;DATABASE=PUBS"
' NOTE: For the "connectstring" line above replace <datasource
' name> with the name of your data source for SQL Server;
' replace <username> with the username used to log on to the data
' source; and replace <password> with the appropriate password.
On Error GoTo Trans_Fail_Err
Set ws = dbengine.Workspaces(0)
Set db = ws.OpenDatabase("", False, False, connectstring)
ws.BeginTrans 'Starts the transaction.
Set rs = db.OpenRecordset("dbo.authors") 'Opens the recordset.
rs.MoveLast
Debug.Print rs![au_lname]
ws.CommitTrans 'Commits the transaction.
rs.Close 'Closes the Recordset.
db.Close
Exit Function
Trans_Fail_Err:
ws.Rollback
If Err = 3146 Then 'ODBC call failed
Error (Err)
Else
MsgBox Error$ 'The message if a different error occurs.
End If
Exit Function
End Function
? Trans_Fail()
Keywords: kbprb kbprogramming kbusage KB131342