Solved

Cannot get workspace  transaction rollback to work

Posted on 2011-09-15
5
402 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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

778 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