Form still saves

ramhanuman

Registered User.
Local time
Today, 13:54
Joined
May 1, 2012
Messages
13
I'm trying to work with the form unload event but even when the user wants to exit the form without saving the information, the info still gets saved into the database.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull([First Name]) Then
Cancel = True
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
If MsgBox("The current information will not save, do you still want to exit?", vbYesNo, "CLOSE") = vbNo Then
Cancel = True
Else
Me.Dirty = False
Me.Undo
End If
End Sub


Why is the form saving the info into the database even though the user wants to exit without saving?
 
It appears what ever activity you do on the form is directly making changes on a table. Ie the change was already made before you choose to close the form.

What are you trying to do on the form ?

Maybe you need an unbound form and have data displayed from a table rather then table data bound to the form.
 
First off, with the lines

Me.Dirty = False
Me.Undo


you're saving the Record to the Table (Me.Dirty = False) and then trying to Undo it! You cannot Undo a saved Record!

If your object here is to ask the user if they want to exit the form, dumping the Record, if the [First Name] field is empty, the code would be something like this:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

If Nz(Me.[First Name], "") = "" Then

 If MsgBox("The current information will not save, do you still want to exit?", vbYesNo, "CLOSE") = vbNo Then
  Cancel = True
  [First Name].SetFocus
 Else
  Me.Undo
  DoCmd.Close
 End If

End If

End Sub
Linq ;0)>
 

Users who are viewing this thread

Back
Top Bottom