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
Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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 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…

829 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