ACC: How to Automatically Detect If a Form Is Being Edited

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

SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

This article describes two techniques you can use to automatically run code when a form's edit mode changes. You can use these techniques when you want to have different controls available on a form when a record is being edited and when a record is not being edited.

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 a pencil is displayed in the record selector, the record is being edited but has not yet been saved. When a triangle is displayed in the record selector, the record has been saved and is not being edited. Note that if the form's RecordSelector property is set to No, these symbols are not displayed.

There are two methods you can use to automatically run code when a form's edit mode changes:

  • Use the form's Timer event to check the form's Dirty property periodically to see if the edit mode has changed.
  • Use the Dirty property in an expression on the form to run a function when the edit mode changes.

The following examples demonstrate how to use these techniques to automatically enable or disable an Undo Record button depending on a form's edit mode.

Method 1: Using the Form's Timer Event

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

  2. Open the Employees form in Design view.

  3. Use the Command Button Wizard to create an Undo Record button on the form.

  4. Change the Undo Record button's properties to match the following:

          Name: btnUndo
          Enabled: No
    

  5. Set the form's TimerInterval property to:

          1000
    

  6. Set the form's OnTimer property to the following event procedure:

          Private Sub Form_Timer()
    
              Static bFlag As Boolean
              If Me.Dirty Then
                  If Not bFlag Then
                      Me!btnUndo.Enabled = True
                      bFlag = True
                  End If
              Else
                  If bFlag Then
                     Me!FirstName.SetFocus
                     Me!btnUndo.Enabled = False
                     bFlag = False
                  End If
              End If
          End Sub
    
    

  7. View the form in Form view. Note that the Undo Record button is unavailable.

  8. Modify any field in the current record, and then press the TAB key. Note that the Undo Record button becomes available.

  9. Press the ESC key twice (or click Undo on the Edit menu) to undo your changes. Note that the Undo Record button becomes unavailable.

Method 2: Using the Dirty Property in an Expression

  1. Follow steps 1-4 in method 1 above.

  2. Create a new text box with the following properties on the form:

          Name: txtEditModeChange
          ControlSource: =[Form].[Dirty] & EditModeChange([Form])
          Visible: No
    

  3. On the View menu, click Code.

  4. Create the following function in the module, and then close the module:

          Function EditModeChange (F As Form) As Variant
    
             If F.Dirty Then
                F!btnUndo.Enabled = True
             Else
                F!btnUndo.Enabled = False
             End If
          End Function
    
    

  5. Set the form's AfterUpdate property to the following event procedure:

          Sub Form_AfterUpdate ()
             Me!txtEditModeChange.Requery
          End Sub
    
    

  6. Follow steps 7-9 in method 1 above to test this method.

REFERENCES

For more information about the Dirty property, search the Help Index for "Dirty property," or ask the Microsoft Access 97 Office Assistant.

For more information about the Timer event, search the Help Index for "Timer Event," or ask the Microsoft Access 97 Office Assistant.

Keywords          : kbusage 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.