Solved

commit/rollback ado transaction

Posted on 2002-06-03
7
1,207 Views
Last Modified: 2008-03-03
Hi,

The following is my vb code using access database.  I try to simulate the rollback action but not all the transaction is fully rollbacked.  Only transaction in UpdBillDetData and UpdBillHdrData can be rollbacked. The transaction in RunSQL1(strSQL1, cnnPhoneData),RunSQL1(strSQL, cnnPhoneData) and RunSQL1(strSQL3, cnnPhoneData) can not be rollbacked.  (fyi,UpdBillDetData and UpdBillHdrData are using recordset.update to update the record) Why? How to solve it?
   

Sub EditRec()
    Dim strSQL As String
    Dim strSQL1 As String
    Dim strSQL2 As String
    Dim strSQL3 As String
    Dim strSQL4 As String
    Dim strSQL5 As String
    Dim SStr As String
    Dim cmd As ADODB.Command
    Dim TmpTbl As String
    Dim rsTmptbl As ADODB.Recordset
    Dim rsBillHdr As ADODB.Recordset

    Set cmd = New ADODB.Command
   
    strSQL = "INSERT INTO " _
           & "BillDetHist " _
           & "Select * " _
           & "FROM BillDet in '" & DataMasPath & "'" _
           & " where " _
           & " BillNbr = '" & Trim(BillNbrTxt.Text) & "'"
 
    strSQL1 = "INSERT INTO " _
           & "BillHdrHist " _
           & "Select * " _
           & "FROM BillHdr in '" & DataMasPath & "'" _
           & " where " _
           & " BillNbr = '" & Trim(BillNbrTxt.Text) & "'" _
           & " and ActiveStatusFlag = '1'"
   
    strSQL3 = "Delete from " _
           & "BillDet " _
           & " where " _
           & " BillNbr = '" & Trim(BillNbrTxt.Text) & "'"
   
    strSQL4 = "Select * from " _
           & "BillHdr " _
           & " where " _
           & " BillNbr = '" & Trim(BillNbrTxt.Text)  "'"        

    strSQL = "INSERT INTO " _
           & "BillDetHist " _
           & "Select * FROM BillDet in '" & DataMasPath & "'" _
           & " where " _
           & " BillNbr = '" & Trim(BillNbrTxt.Text) & "'"

    Set rsBillHdr = RunSQLReturnRSforUpd(strSQL4, , cnnPhoneData)

cnnPhoneData.BeginTrans
 cnnPhoneTmpData.BeginTrans
    'Insert into BillHdrHist
    Call RunSQL1(strSQL1, cnnPhoneData)
   
    'Insert into BilldetHist
    Call RunSQL1(strSQL, cnnPhoneData)
   
    'Del BillDet
    Call RunSQL1(strSQL3, cnnPhoneData)
 
    'insert record into BillDet table  
    'from tmpbilldetaildata
    Call UpdBillDetData
    Call UpdBillHdrData(rsBillHdr)
    cnnPhoneTmpData.RollbackTrans
    cnnPhoneData.RollbackTrans
    'simulate the rollback trx
'cnnPhoneData.CommitTrans
    Set rsBillHdr = Nothing
   
    Exit Sub
   

End Sub


Sub RunSQL1(ByVal strSQL As String, Optional ByVal strConn As String)
On Error GoTo Err_RunSQL
   
    ' Create the ADO objects
    Dim cmd As ADODB.Command
    Set cmd = New ADODB.Command

    ' Init the ADO objects & the stored proc parameters
    cmd.ActiveConnection = strConn
   
    With cmd
        .CommandText = strSQL
        .CommandType = adCmdText
    End With
   
    ' Execute the query without returning a recordset
    cmd.Execute , , adExecuteNoRecords
   
    ' Cleanup
    cmd.ActiveConnection = Nothing
    Set cmd = Nothing
    Exit Sub
   
End Sub
 
0
Comment
Question by:chee68
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 1

Expert Comment

by:killer5
ID: 7053172
maybe because you are using a different connection in RunSQL1 procedure.

use either one of the following connections....
cnnPhoneTmpData
cnnPhoneData  


can i take a look at your UpdBilLDetData procedure?

0
 
LVL 1

Expert Comment

by:killer5
ID: 7053190
maybe because you are using a different connection in RunSQL1 procedure.

use either one of the following connections....
cnnPhoneTmpData
cnnPhoneData  


can i take a look at your UpdBilLDetData procedure?

0
 

Author Comment

by:chee68
ID: 7053289
Actually, i need both connection,cnnPhoneData and cnnPhoneTmpData.  These 2 connection connected to 2 diferrent database.  During this action, both database will be updated if succesfull or else need to rollback.

The following is as per your request

Sub UpdBillDetData()
'TmpBillDetailData is connected to cnnPhoneTmpData
'rsBillDet is connected to cnnPhoneData
        TmpBillDetailData.Recordset.MoveFirst
        Do While Not TmpBillDetailData.Recordset.EOF
            rsBillDet.AddNew
            rsBillDet("BillNbr") = BillNbrTxt.Text
            rsBillDet("PONbr") = PONbrTxt.Text
            rsBillDet("ItemNbr") = TmpBillDetailData.Recordset("ItemNbr")
            rsBillDet("StockId") = TmpBillDetailData.Recordset("StockId")
            rsBillDet("SerialNbr") = TmpBillDetailData.Recordset("SerialNbr")
            rsBillDet("Qty") = TmpBillDetailData.Recordset("Qty")
            rsBillDet("UnitPrice") = TmpBillDetailData.Recordset("UnitPrice")
            rsBillDet("AmountAfterTax") = TmpBillDetailData.Recordset("AmountAfterTax")
            rsBillDet("Description") = TmpBillDetailData.Recordset("Description")
            TmpBillDetailData.Recordset.MoveNext
            rsBillDet.Update
        Loop
        TmpBillDetailData.Refresh
        DataGrid1.ReBind
        DataGrid1.Refresh

End Sub
0
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

 
LVL 5

Expert Comment

by:rpai
ID: 7053750
Is this an MTS object? In that case objContext.SetAbort should do the required.
0
 
LVL 5

Accepted Solution

by:
rpai earned 100 total points
ID: 7053787
In your RunSQL1 Subroutine, replace this::
Sub RunSQL1(ByVal strSQL As String, Optional ByVal strConn As String)
BY
Sub RunSQL1(ByVal strSQL As String, Optional ByVal strConn As ADODB.Connection)

This should resolve the issue with the RollbackTrans you have.

Cheers!
0
 

Author Comment

by:chee68
ID: 7055178
excellent answer!!!!Thank you very much!I should post the question earlier so that i do not need crack my head for so long.Thanks again!
0
 

Author Comment

by:chee68
ID: 7055180
excellent answer!!!!Thank you very much!I should post the question earlier so that i do not need crack my head for so long.Thanks again!
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

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

734 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