Full Version: Undo , Save buttons with the On Dirty event
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
jmal
I have a very large form where I have added three command buttons:

Undo - Save - Close

I want the Undo & Save Button to be disabled until the form becomes dirty when a record has been changed. Then the Undo & Save buttons become enabled and the Close button becomes disables until one of the Undo & Save buttons is selected.


At the moment I am using the following code in the "On Timer" event of the form which seems to be working sometimes but not all the time and I have read that using the "On Timer" is not the best solution for alot of code.

Here's my code:

Private Sub Form_Timer()
On Error Resume Next
Static bFlag As Boolean
If Me.Dirty Then
If Not bFlag Then
Me!btnUndo.Enabled = True
Me!butSave.Enabled = True
Me!ExitPropertiesForm.Enabled = False
bFlag = True
End If
Else
If bFlag Then
'Me.ViewReports.SetFocus
Me!btnUndo.Enabled = False
Me!butsave.Enabled = False
Me!ExitPropertiesForm.Enabled = True
bFlag = False
End If
End If

End Sub

------------------------------------------------------------------------------------------------------------

should I be using something similar to this in a different event (e.g After Update event)
Larry Larsen
Hi
You could use the "On Dirty" event of the form to tigger your code.
Here is an example from the help file from the "On Dirty" property.

Dirty Event — Event Procedures Example

The following example enables the btnUndo button when data is changed. The UndoEdits( ) subroutine is called from the Dirty event of text box controls. Clicking the enabled btnUndo button restores the original value of the control by using the OldValue property.

Private Sub Form_Dirty()
If Me.Dirty Then
Me!btnUndo.Enabled = True ' Enable button.
Else
Me!btnUndo.Enabled = False ' Disable button.
End If
End Sub

Sub btnUndo_Click()
Dim ctlC As Control
' For each control.
For Each ctlC in Me.Controls
If ctlC.ControlType = acTextBox Then
' Restore Old Value.
ctlC.Value = ctlC.OldValue
End If
Next ctlC
End Sub

HTH's
thumbup.gif
jmal
Hi Larry

Tried the above but the problem im having is when I edit a record the Undo button becomes enabled, After i select the undo button to undo the changes this button remains enabled. Where can I get this to become disabled until the "On Dirty" event is reached again
Larry Larsen
Hi
I seem to be having a day where nothings working, I tried to emulate the code example on a simple form/table and can't seem to trigger the dirty event to "True" when I edit a simple control/field on the form.

You may (not tested) be able to call the forms dirty event after you have clicked the undo button..?
eg:
Call Form_Dirty()

HTH's
thumbup.gif
Chaga
Try this


Private Sub Form_Dirty(Cancel As Integer)

Me!btnUndo.Enabled = True
Me!butSave.Enabled = True
Me!ExitPropertiesForm.Enabled = False

End Sub



Sub btnUndo_Click()
Dim ctlC As Control
' For each control.
For Each ctlC In Me.Controls
If ctlC.ControlType = acTextBox Then
' Restore Old Value.
ctlC.Value = ctlC.OldValue
End If
Next ctlC
Me!Anyfield.SetFocus 'You need to lose the focus from the button to disable it;
Me!btnUndo.Enabled = False
Me!butSave.Enabled = False
Me!ExitPropertiesForm.Enabled = True
End Sub



You can also add some code for the save button to disable it and the undo button.

HTH
jmal
Hi All

I have being playing around with this for a while and this is what ive come up with.

Ive added to the following code:
----------------------------------------------------
Public Sub Form_Dirty(Cancel As Integer)

Me!btnUndo.Enabled = True ' Disable button.
Me!btnSave.Enabled = True
Me!ExitPropertiesForm.Enabled = False

End Sub

--------------------------------------------------------

Private Sub btnUndo_Click()

DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70

Me.ExitPropertiesForm.Enabled = True
Me.ExitPropertiesForm.SetFocus

End Sub
------------------------------------------------------------

Private Sub btnSave_Click()

DoCmd.SetWarnings False

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

DoCmd.SetWarnings True

Me.ExitPropertiesForm.Enabled = True
Me.ExitPropertiesForm.SetFocus

End Sub

-----------------------------------------------------------------------
Private Sub ExitPropertiesForm_GotFocus()

Me.btnUndo.Enabled = False
Me.btnSave.Enabled = False
End Sub
---------------------------------------------------------------------

This seems to be working pritty good except when I open the form acFirst Record is selected.

When this record is edited it does not trigger the (On Dirty) event alot of the time.

If i select another record on the form, the code works perfectly. STRANGE??

Any suggestions
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.