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

ID: Q210334


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

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

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


SUMMARY

This article describes two techniques that 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. 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 Access displays a pencil symbol in the record selector, the record is being edited, but it has not yet been saved. When Access displays a triangle symbol 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 that 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.


  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 as follows:


  5. 
       Name: btnUndo
       Enabled: No 
  6. Set the form's TimerInterval property to 1000.


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


  8. 
    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 
  9. View the form in Form view. Note that the Undo Record button is dimmed.


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


  11. Click Undo on the Edit menu to undo your changes. Note that the Undo Record button becomes dimmed.


Method 2: Using the Dirty Property in an Expression

  1. Follow steps 1 through 4 in Method 1.


  2. Add a new text box with the following properties to the form:


  3. 
       Name: txtEditModeChange
       ControlSource: =[Form].[Dirty] & EditModeChange([Form])
       Visible: No 
  4. On the View menu, click Code.


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


  6. 
    Function EditModeChange (F As Form) As Variant
    
       If F.Dirty Then
          F!btnUndo.Enabled = True
       Else
          F!btnUndo.Enabled = False
       End If
    
    End Function 
  7. Set the form's AfterUpdate property to the following event procedure:


  8. 
    Sub Form_AfterUpdate ()
       Me!txtEditModeChange.Requery
    End Sub 
  9. Follow steps 7 through 9 in Method 1 to test this method.



REFERENCES

For more information about the Dirty property, click Microsoft Access Help on the Help menu, type the Dirty property in the Office Assistant or the Answer Wizard, and then click Search to view the topic.


For more information about Timer event, click Microsoft Access Help on the Help menu, type Timer event in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Additional query words:

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


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