ACC2000: How to Create an AfterUndo Form Event

ID: Q210326


The information in this article applies to:
  • Microsoft Access 2000

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

This article applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp).


SUMMARY

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. Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp


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:


  2. 
       Name: txtEditModeChange
       ControlSource: =[Form].[Dirty] & CheckUndo([Form])
       Visible: No 
  3. Add the following event procedure to the AfterUpdate property of the form:


  4. 
    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 
  5. Create a module and type the following line in the Declarations section:


  6. 
    Option Explicit
    Dim PrevBookmark 
  7. Type the following procedure:


  8.  
    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.


  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:


  4. 
    Sub AfterUndo ()
       MsgBox "Record Changes Undone"
    End Sub 
  5. View the form in Form view.


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


  7. On the Edit menu, click Undo Current Field/Record. Note that the "Record Changes Undone" message box appears.


  8. On the Edit menu, click Go To, and then click New Record.


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


  10. 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:

Q210334 How to Automatically Detect If a Form Is Being Edited.
For more information about undoing changes, click Microsoft Access Help on the Help menu, type undoing changes when editing records in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Additional query words: onundo

Keywords : kbusage kbdta AccCon FmsEvnt FmsHowto
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbinfo


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