Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prompt user to see if they want to save changes to a record 2

Status
Not open for further replies.

montyb

Technical User
Sep 8, 2000
26
0
0
US
I would like to prompt users after they change/create a record on a form to verify that they really want the changes. Here is the code I have...

Private Sub Form_AfterUpdate()
Dim s As Byte
s = MsgBox("Do you want to save your changes?", vbYesNo, "Save")
If s = vbYes Then
On Error GoTo SAExit
On Error Resume Next
DoCmd.RunCommand acCmdSaveRecord
SAExit:
If Err <> 0 Then
MsgBox Error$, vbCritical
End If
Exit Sub
Else
If Me.Dirty Then Me.Undo
End If
End Sub

The only problem is when the user selects no, Me.Dirty is showing false when I step through the code even when the records have been changed (the changes are not getting undone). What am I doing wrong or what other options do I have.

thanks in advance
 
First of all, if you're using the dirty property you must do it for every edited control on the form.

Second, when using the dirty property use the oldvalue along with it not undo.

Third, The first On Error will never happen.

Fourth, the help has a very good example of the dirty usage.

HTH

PsychPt@Hotmail.com
 
You need to do this on &quot;BeforeUpdate&quot; not &quot;AfterUpdate&quot;. The BeforeUpdate allows you to &quot;Cancel&quot; the update.

==========

First I have a button that simply closes the form (DoCmd.Close). I disable the Access close button (x in the upper right corner) on the form, and use the button to make sure the user only has one way out. Then I use the following code:

==========
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Dirty Or Me.NewRecord Then
If MsgBox(&quot;Do you wish to save changes&quot;, 36, &quot; Continue?&quot;) = vbNo Then
Cancel = True
End If
End If
End Sub
========== Jim Lunde
compugeeks@hotmail.com
Custom Application Development
 
Thank you both for the help!
 
The 'Dirty' property in Access 97 referred to controls only. In Access 2000 the form object has a 'Dirty' property. Me.Dirty would work in any Access 2000 application.

Steve King Growth follows a healthy professional curiosity
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top