Results 1 to 3 of 3

Thread: BeginTrans - CommitTrans

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2001
    Posts
    1,075

    BeginTrans - CommitTrans

    I'm using an ADO 2.5 reference/VB6/Access2

    I'm a little confused about the proper way to use the BeginTrans, CommitTrans, and RollBackTrans in my situation. In the ADO help file on the MSDN CD they show a situation where you open a record set, then loop through and make changes to some records, and then ask the user to CommitTrans.

    In my situation I need to UPDATE records in one table and then DELET records in another table. Would I have one transaction with one set of BeginTrans-CommitTrans, or should I have Two sets of BeginTrans - CommitTrans, one nested inside the other.

    VB Code:
    1. 'Option One
    2.     objConn.BeginTrans
    3.    
    4.     sSQL = "UPDATE TableOne SET MyID = " & iNewID & " WHERE MyID = " & iOldID    
    5.     objCmd.ActiveConnection = objConn
    6.     objCmd.CommandText = sSQL
    7.     objCmd.CommandType = adCmdText
    8.     objCmd.Execute lRows1
    9.      
    10.      sSQL = "DELETE FROM TableTwo WHERE MyID = " & iOldID                        
    11.      objCmd.ActiveConnection = objConn
    12.      objCmd.CommandText = sSQL
    13.      objCmd.CommandType = adCmdText
    14.      objCmd.Execute lRows2
    15.      
    16.     If MsgBox(lRows1 + lRows2 & " Will be changed or deleted.", vbYesNo, App.Title) = vbYes Then
    17.         objConn.CommitTrans
    18.     Else
    19.         objConn.RollbackTrans
    20.     End If
    21.  
    22.  
    23.     'Option Two
    24.     objConn.BeginTrans
    25.    
    26.     sSQL = "UPDATE TableOne SET MyID = " & iNewID & " WHERE MyID = " & iOldID    
    27.     objCmd.ActiveConnection = objConn
    28.     objCmd.CommandText = sSQL
    29.     objCmd.CommandType = adCmdText
    30.     objCmd.Execute lRows1
    31.  
    32.     objConn.BeginTrans    
    33.      sSQL = "DELETE FROM TableTwo WHERE MyID = " & iOldID                        
    34.      objCmd.ActiveConnection = objConn
    35.      objCmd.CommandText = sSQL
    36.      objCmd.CommandType = adCmdText
    37.      objCmd.Execute lRows2
    38.      
    39.     If MsgBox(lRows1 + lRows2 & " Will be changed or deleted.", vbYesNo, App.Title) = vbYes Then
    40.         objConn.CommitTrans
    41.         objConn.CommitTrans
    42.     Else
    43.         objConn.RollbackTrans
    44.         objConn.RollbackTrans
    45.     End If

    Greg
    Free VB Add-In - The Reference Librarian
    Click Here for screen shot and download link.

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    hmmm

    Just the one should do it

    All the transactions do is record the changes, so as long as you aren't changing lots of data it should cope with it. The idea being if something goes wrong you roll back the previous data before you changed it. So processing your records then deleting them in the same transcaction should be ok.

    I'd suggest (if you have the time) doing both one for it all and two and comparing the results.

    Regards

    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2001
    Posts
    1,075
    I'd suggest (if you have the time) doing both one for it all and two and comparing the results.
    Thanks for the feedback. I probably will test. I'm not dealing with a lot of data, and this is a single user app, so my main concern was finding the "proper" way to do it. It seems that sometimes there is more than one way to do things but only one preferred method.

    Greg
    Free VB Add-In - The Reference Librarian
    Click Here for screen shot and download link.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width