Microsoft KB Archive/210334

= HOW TO: Automatically Detect If a Form Is Being Edited in Access 2000 =

Article ID: 210334

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q210334



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

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

IN THIS TASK
SUMMARY
 * Method 1: Using the Form's Timer Event
 * Method 2: Using the Dirty Property in an Expression

REFERENCES



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.

NOTE: This article explains a technique demonstrated in the sample file, FrmSmp00.mdb. For information about how to obtain this sample file, please see the following article in the Microsoft Knowledge Base:

233324 ACC2000: Microsoft Access Sample Forms Database Available in Download Center

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements.

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.

back to the top

Method 1: Using the Form's Timer Event
 Open the sample database Northwind.mdb. Open the Employees form in Design view. Use the Command Button Wizard to create an Undo Record button on the form.  Change the Undo Record button's properties as follows:   Name: btnUndo Enabled: No                    Set the form's TimerInterval property to 1000.  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 </li> View the form in Form view. Note that the Undo Record button is dimmed.</li> Modify any field in the current record, and then press the TAB key. Note that the Undo Record button becomes available.</li> Click Undo on the Edit menu to undo your changes. Note that the Undo Record button becomes dimmed.</li></ol>

back to the top

Method 2: Using the Dirty Property in an Expression
<ol> Follow steps 1 through 4 in Method 1.</li>  Add a new text box with the following properties to the form: <pre class="fixed_text">  Name: txtEditModeChange ControlSource: =[Form].[Dirty] & EditModeChange([Form]) Visible: No                   </li> On the View menu, click Code.</li>  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 </li>  Set the form's AfterUpdate property to the following event procedure: Sub Form_AfterUpdate Me!txtEditModeChange.Requery End Sub </li> Follow steps 7 through 9 in Method 1 to test this method.</li></ol>

back to the top

<div class="references_section">