Solved

Cannot get workspace  transaction rollback to work

Posted on 2011-09-15
5
400 Views
Last Modified: 2012-06-27
In the code attached the updates work fine but I cannot get the rollback to work.

Any ideas Lou.
Private Sub cmdUpdatePrices_Click()

     
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim wrkCurrent As DAO.Workspace
    Dim blnTrans As Boolean   'Flag that transaction is active.
    Dim strFieldName As String
    Dim strTextBoxName As String
    Dim strSubTextBoxName As String
  
    On Error GoTo ITAError
    
    
    strFieldName = "PR_Bronze"
    strSubTextBoxName = "txtPR_Bronze"
       
   'Perfom the updates
    Set wrkCurrent = DBEngine.Workspaces(0)
    Set db = CurrentDb()
    Set rst = Me.sfrmPrices.Form.RecordsetClone
    
    wrkCurrent.BeginTrans 'Begin the transaction
    blnTrans = True
       
      'update by percentage
             strTextBoxName = "txtPlusMinusPercent"
                  If Not IsNull(Me.Controls(strTextBoxName)) Then
                     If rst.RecordCount > 0 Then
                         rst.MoveFirst
                         Do While Not rst.EOF
                             rst.Edit
                                 rst(strFieldName) = Nz(rst(strFieldName), 0) * (1 + Me.Controls(strTextBoxName))
                             rst.Update
                             If rst(strFieldName) < 0 Then
                                    Call MsgBox("The entry you have made would result in one or more negative prices. Please adjust the entry and try again.", _
                                    vbInformation, "Yorwaste Master Pricing Matrix")
                                    GoTo ITAExit
                             End If
                             rst.MoveNext
                        Loop
                        wrkCurrent.CommitTrans
                        blnTrans = False
                     End If
                GoTo ITAExit
                End If

ITAExit:
        On Error Resume Next ' Make sure clean up is done even if there is an error. See http://allenbrowne.com/ser-37.html
        If blnTrans Then   'Rollback if the transaction is active.
            wrkCurrent.Rollback
        Else:  wrkCurrent.CommitTrans
        End If
        Set rst = Nothing
        Set wrkCurrent = Nothing
    Me.Requery
    Exit Sub

ITAError:
    MsgBox Err.Number & " " & Err.Description, , Err.Source
    Resume ITAExit


End Sub

Open in new window

0
Comment
Question by:Louverril
  • 2
  • 2
5 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36544436
perhaps, because you already committed the transaction with

   wrkCurrent.CommitTrans


you may need to place the commit or rollback in an   if ... then  condition block
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36544484
revising your codes, adding the confirmation for committing the changes

                         Do While Not rst.EOF
                             rst.Edit
                                 rst(strFieldName) = Nz(rst(strFieldName), 0) * (1 + Me.Controls(strTextBoxName))
                             rst.Update
                             If rst(strFieldName) < 0 Then
                                    Call MsgBox("The entry you have made would result in one or more negative prices. Please adjust the entry and try again.", _
                                    vbInformation, "Yorwaste Master Pricing Matrix")
                                    GoTo ITAExit
                             End If
                             rst.MoveNext
                        Loop
     

    ' Ask if the  changes mafe will be committed
      If MsgBox("Save all changes?", vbYesNo) = vbYes Then
         wrkCurrent.CommitTrans

      Else
         wrkCurrent.Rollback
      End If

Open in new window

0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 36544610
Lou,

  That's not going to work because forms in Access do their work in their own workspace.  

See

ACC2000: How to Control Bound Form Transactions in Access Databases
http://support.microsoft.com/kb/248011

Jim.
0
 

Author Comment

by:Louverril
ID: 36544753
Capricorn - In the code I attached,  I don't commit the changes if the amount is less than zero - it goes to the exit code with blnTrans still as True. That fires the rollback - but it doesn't actually  rollback.

Jim
Thanks for this. It will be handy to know in the future . For now I have altered to code to loop through and check for negatives first: - along the lines below. This works fine - although maybe I am doing more processing than if I tried to follow the method in example you inked to.

Thanks both,

Lou

'update by percentage
             strTextBoxName = "txtPlusMinusPercent"
            If Not IsNull(Me.Controls(strTextBoxName)) Then
                 If rst.RecordCount > 0 Then
                     rst.MoveFirst
                      'first check if any negative values will result
                      Do While Not rst.EOF
                          lngAmount = Nz(rst(strFieldName), 0) * (1 + Me.Controls(strTextBoxName))
                          If lngAmount < 0 Then ' message and exit
                                 Call MsgBox("The entry you have made would result in one or more negative prices. Please adjust the entry and try again.", _
                                 vbInformation, "Yorwaste Master Pricing Matrix")
                                 GoTo ITAExit
                          End If
                          rst.MoveNext
                     Loop
                    'Perform the updates
                    rst.MoveFirst
                    Do While Not rst.EOF
                         rst.Edit
                             rst(strFieldName) = Nz(rst(strFieldName), 0) * (1 + Me.Controls(strTextBoxName))
                         rst.Update
                         rst.MoveNext
                    Loop
                End If
                GoTo ITAExit
            End If

ITAExit:
      
    Set rst = Nothing
    Me.Requery
    Exit Sub
     
ITAError:
    MsgBox Err.Number & " " & Err.Description, , Err.Source
    Resume Next

End Sub

Open in new window

0
 

Author Closing Comment

by:Louverril
ID: 36549302
Thnaks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

920 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now