?
Solved

Cannot get workspace  transaction rollback to work

Posted on 2011-09-15
5
Medium Priority
?
412 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
[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
  • 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 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

752 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