You are here:

Using MS Access/Event which fires after undo?

Advertisement


Paul wrote at 2009-06-03 14:49:48
The problem with that is that the Undo event fires Before the undo has taken place so in most cases its useless because most of the time you need to refresh the UI based on the current data not the data that is about to be undone.


jhaubold wrote at 2009-10-28 14:42:09
Here is how, using a global variable, you can run code after the Undo event.



Here is the code:



'define global variable

Dim boolTemp as boolean

.

.

.





Private Sub Form_Undo(Cancel As Integer)



   If boolTemp = False Then

       boolTemp = True

       Me.Undo

       'after undo code goes here

  .

  .

  .

   else

  boolTemp = False

   End If

   

End Sub


ahall wrote at 2010-05-23 22:02:10
Well Scott your assumption is wrong! As the orig. poster explained the value of the controls have not been reset to their old values.



I'm NOT surprised by this given the name of the event and the fact that the Undo event is cancelable.


Phibbus wrote at 2011-05-05 00:51:18
About two years late, but I have just run into this same problem (and yes, I too was surprised I had never encountered it before.)



As you stated in reply to Scott, if you attach code to the form's Undo event handler and query the form's data from therein (whether directly using Me or via the Recordset or RecordsetClone)that data will NOT yet reflect the Undo (that is, it will still be the Dirty data.)  Scott's solution of calling the form's Current event handler from within the Undo event also does not work and will still get you pre-undone data.



My final solution was to actually trigger another Undo from within the Undo event code, itself.  You'll want to have a module-scope variable to track the triggered undo so that code does not get stuck in an undo-loop and potentially run out of stack space. So, in the declarations for you form's module, put something like:



    Dim mfUndone as Boolean



Set this to False in your forms's Open event code.  Then, in the code for the Undo event:



    If Not mfUndone Then

        mfUndone = True

        Me.Undo

    End if

    mfUndone = False



The Me.Undo (or simply "Undo") will re-trigger the Undo event and cause the code to execute from the beginning, however Undo will not fire again the second time around due to setting the mfUndone variable to True before firing it the first time.  You then set it to False again directly after the conditional to achieve a fresh state.  Any code you place AFTER this bit (and thus, after the second undo) that queries the form's data WILL, in fact, reflect the undone changes.




Jules wrote at 2016-08-04 10:46:52
7 years late but I had the same problem and this post still shows up on google so here's what I believe is the correct solution.

Use the TextBox.oldValue property to get the undone value of the data.

This shows what the values will be set back to after the Undo event has completed.


Using MS Access

All Answers


Answers by Expert:


Ask Experts

Volunteer


Scottgem

Expertise

I can answer almost all types of questions relating to Microsoft Access usage and application design. My strengths are database and interface design.

Experience

I've been designing databases for over 25 years working with dBase, FoxPro, Approach and Access.

Organizations
Author of Microsoft Office Access 2007 VBA
Techncial Editor for Special Edition Using Microsoft Access 2007 and Access 2007 Forms, Reports & Queries From Que Publishing

Education/Credentials
Brooklyn College BA

©2016 About.com. All rights reserved.