Solved

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

Posted on 2001-06-05
5
160 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

831 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