Solved

Cannot get workspace  transaction rollback to work

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

691 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