Solved

How do I make a series of ADO sql statements atomic??

Posted on 2001-06-05
5
158 Views
Last Modified: 2013-11-23
I have a series of 5 sql executes with my ADO object and I need them to either all execute or on error I need to rollback everyone that has previously executed.
0
Comment
Question by:mccainz2
5 Comments
 
LVL 6

Expert Comment

by:dbirdman
ID: 6157853
Use the ADO Connection object for Transaction Processing.
Prior to executing your sql statements, begin the transaction with:  cn.BeginTrans.  If one of the sql executes gets an error, use cn.RollbackTrans.  If they all complete successfully, use cn.CommitTrans.  I'm using this now in an app and it works great.

Good Luck
0
 
LVL 6

Accepted Solution

by:
dbirdman earned 50 total points
ID: 6157897
This may be a better examplen for the transaction.  The connection string is for SQL Server; just substitute it for the appropriate connection if you're not using SQL Server.  The Rollback will also handle any updates you may have done to a table within the Transaction.

Public Function Test() As Boolean

Dim cn As ADODB.Connection
Dim rs1 As ADODB.Recordset
Dim rs2 As ADODB.Recordset
Dim strConnection As String
Dim sql1 As String
Dim sql2 As String

Set cn = New ADODB.Connection
Set rs1 = New ADODB.Recordset
Set rs2 = New ADODB.Recordset

cn.ConnectionString  = "Provider=SQLOLEDB;DataSource=MyServer;Initial Catalog=My Database; User ID=MyID; Password=MyPW"

cn.Open

sql1 = "Select * from Table1"
sql2 = "Select * from Table2"

'Begin Transaction
cn.BeginTrans

rs1.Open sql1, cn, adOpenDynamic, adLockPessimistic, adCmdText
  'Stop, for example, if no records are retrieved
If rs1.EOF Then
     cn.RollBackTrans
     Exit Function
End If

rs2.Open sql2, cn, adOpenDynamic, adLockPessimistic, adCmdText
  'Stop again if no records are retrieved
If rs2.EOF Then
     cn.RollBackTrans
     Exit Function
End If

'If no problems, commit the transaction
cn.CommitTrans

Test = True

End Function

Hope this helps

0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6158478
You can have a error handler, and in the error handler, you can place the rollback code, so that, if any error happens when executing one of the sql statements or whatever, then a rollback would occur.

The code could be something like this:

Dim lTrans As Boolean

On Error Go To ErrHandler
lTrans = False
:
:
Conn.Begintrans
lTrans = True
:
:
your sql statements here
:
:
Conn.CommitTrans
lTrans = False
:

ErrHandler:
If Err.Number <> 0 Then
   Msgbox "Could Not Proceed." & Err.Description & " :" & Err.Number
   If lTrans Then
     Conn.RollbackTrans
     lTrans = False
   End If
End If
0
 
LVL 1

Expert Comment

by:mukunth
ID: 6159124
As suggested in other comments u can use the transaction as follow:

cn.Begintrans
.
.
.
(execute the 5 SQL statements here by saying cn.Execute )
cn.execute "insert into test values(1)"
.
.
If cn.Errors.Count > 0 Then
cn.RollBackTrans
Msgbox "errors occured"
else
cn.CommitTrans
End If

where:
cn is the connection object
Hope this helps.
Cheers.
0
 
LVL 5

Author Comment

by:mccainz2
ID: 6160063
danka, for some idiotic reason I was assuming that on error it would behave by rolling back without me actually writing any error handling code....silly goose.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now