Solved

Cannot get workspace  transaction rollback to work

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

707 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

12 Experts available now in Live!

Get 1:1 Help Now