ACC: How to Create an AfterUndo Form Event

Last reviewed: October 24, 1997
Article ID: Q123595
The information in this article applies to:
  • Microsoft Access 2.0, 7.0, 97

SUMMARY

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

This article demonstrates 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:

   ARTICLE-ID: Q150895
   TITLE     : ACC95: Microsoft Access Sample Forms Available on MSL

   ARTICLE-ID: Q175066
   TITLE     : ACC97: Microsoft Access 97 Sample Forms Available on MSL

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.

Follow these steps to create the AfterUndo procedure:

  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 form's AfterUpdate property:

          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 form module's Declarations section 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-4 above.

  3. Modify the code in the AfterUndo procedure (created in step 4 above) 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:

   ARTICLE-ID: Q122294
   TITLE     : 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 FmsHowTo FmsEvnt
Version : 2.0 7.0 97
Platform : WINDOWS
Hardware : x86
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 24, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.