Solved

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

Posted on 2001-06-05
5
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

688 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