ACC: How to Create an AfterUndo Form Event

ID: Q123595


The information in this article applies to:
  • Microsoft Access versions 2.0, 7.0, 97


SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article shows you how to create and use a form module procedure called AfterUndo. The AfterUndo procedure runs when edits made to the current record are undone.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access version 2.0. For more information about Access Basic, please refer to the "Building Applications" manual.

NOTE: This article explains a technique demonstrated in the sample files, FrmSampl.exe (for Microsoft Access for Windows 95 version 7.0) and FrmSmp97.exe (for Microsoft Access 97). For information about how to obtain these sample files, please see the following articles in the Microsoft Knowledge Base:

Q150895 ACC95: Microsoft Access Sample Forms Available in Download Center

Q175066 ACC97: Microsoft Access 97 Sample Forms Available in Download Center


MORE INFORMATION

When you click Undo Record on the Edit menu or press the ESC key twice to undo changes to the current record in a form, there is no built-in form event that is triggered. The AfterUndo procedure simulates an AfterUndo event so that you can restore calculations that may have been set while the record was being edited.

Creating the AfterUndo Procedure

To create the AfterUndo procedure, follow these steps:

  1. Open the form to which you want to add the AfterUpdate procedure in Design view, and then add a text box with the following properties to the form:
    Name: txtEditModeChange
    ControlSource: =[Form].[Dirty] & CheckUndo([Form])
    Visible: No


  2. Add the following event procedure to the AfterUpdate property of the form:
    
          Sub Form_AfterUpdate ()
          ' Because the record is being saved, which changes the edit mode, the
          ' bookmark should be reset so that it will appear to the CheckUndo()
          ' function that the user moved to another record.
             PrevBookmark = Null
          End Sub 


  3. Add the following lines to the Declarations section of the form module if they are not already there:
    
          Option Explicit
          Dim PrevBookmark 


  4. Add the following procedures to the form module:
    
          Function CheckUndo (F As Form) As Variant
             Dim CurrBookmark
    
             ' Is the record clean (not dirty)?
             If Not F.Dirty Then
                ' If so, get the current bookmark.
                On Error Resume Next
                CurrBookmark = F.bookmark
    
                ' If an error occurred, this is the new record.
                If Err Then CurrBookmark = "NewRecord"
    
                ' Determine if the edit change occurred on the same record (the
                ' record was undone, as opposed to moving to another record).
                 If StrComp(CurrBookmark, PrevBookMark, 0) = 0 Then
                   ' The record was undone.
                   AfterUndo
                Else
                   ' The record was not undone (moved to another
                   ' record). Record the bookmark of the current
                   ' record for the next iteration.
                   PrevBookmark = CurrBookmark
                End If
             End If
    
          End Function
    
          Sub AfterUndo ()
            ' Add the code you want to run when the record is undone here.
          End Sub 


How to Use the AfterUndo Procedure

The following example demonstrates how to use the AfterUndo procedure:

  1. Open the sample database Northwind.mdb (or NWIND.MDB in version 2.0).


  2. Open the Employees form in Design view and follow steps 1 through 4 of the "Creating the AfterUndo Procedure" section earlier in this article.


  3. Modify the code in the AfterUndo procedure (created in step 4) as follows:
    
          Sub AfterUndo ()
             MsgBox "Record Changes Undone"
          End Sub 


  4. View the form in Form view.


  5. Modify any field in any record in the form.


  6. On the Edit menu, click Undo Current Field/Record (Undo Current Record in Microsoft Access 2.0 and 7.0). Note that the "Record Changes Undone" message box appears.


  7. On the Edit menu, click Go To, and then click New Record (click New in Microsoft Access 2.0 and 7.0).


  8. Type any text in any field in the new record.


  9. Press the ESC key. Note that the "Record Changes Undone" message box appears.



REFERENCES

For more information about determining whether a record on a form is being edited, please see the following article in the Microsoft Knowledge Base:

Q122294 ACC: How to Automatically Detect If a Form Is Being Edited
For more information about undoing changes, search the Help Index for "undoing changes when editing records," or ask the Microsoft Access 97 Office Assistant.

Additional query words: onundo

Keywords : kbusage FmsEvnt FmsHowto
Version : WINDOWS:2.0,7.0,97
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: November 2, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.