Close without saving

  • Thread starter Thread starter TRob
  • Start date Start date
T

TRob

I want to enable a user of a form to exit the form without
saving the data they have input. For example, they may
have filled out a few of the fields on the form, but for
whatever reason, they need to be able to exit the form and
not save the record.

Surely there is a simple way to do this, but I can't find
it. I have experimented with code on a command button
using "End", "Unload", but none work.

Thanks
 
Try the following code in the command button's Click
eventhandler
DoCmd.Close acForm, Me.Name

Hope This Helps
Gerald Stanley MCSD
 
Try the following code in the command button's Click
eventhandler

'Precede the "Close" command in your "Cancel" button's code with:
If Me.Dirty Then
'Undo all changes in the form
Me.Undo
End If

'Now, close the form
 
I tried this code before and it closes the form just fine,
but the record they started entering is saved. I do not
want the record to be saved when the form is closed.
 
If you'd like to prompt the user to make sure they want to exit without saving, try this code:

Private Sub cmdCloseNoSave_Click()
On Error GoTo ErrorPoint

If Me.Dirty = True Then
If MsgBox("Do you wish to close the form without saving " _
& "your changes?", vbInformation + vbYesNo + vbDefaultButton2, _
"Discard Changes?") = vbYes Then
Me.Form.Undo
DoCmd.Close acForm, Me.Name
End If
Else
DoCmd.Close acForm, Me.Name
End If

ExitPoint:
Exit Sub

ErrorPoint:
MsgBox "The following error has occurred:" _
& vbNewLine & "Error Number: " & Err.Number _
& vbNewLine & "Error Description: " & Err.Description _
, vbExclamation, "Unexpected Error"
Resume ExitPoint

End Sub
 
Darn. It didn't work. And your code looked so nice! It
appears to just undo the changes that were just made.

I see now that I wasn't thinking about the whole picture.
My problem probably stems from the way the record exists.
The record I'm testing is one that was created by first
clicking on a command button "Add new record". THIS is
the action that actually places the record into the table,
not the changes I was dumping when your code says "Undo".

I'll just rethink this and probably add a "Delete Record"
option if they need to get rid of the record.

Thank you!
-----Original Message-----
If you'd like to prompt the user to make sure they want
to exit without saving, try this code:
 
Darn. It didn't work. And your code looked so nice! It
appears to just undo the changes that were just made.

I see now that I wasn't thinking about the whole picture.
My problem probably stems from the way the record exists.
The record I'm testing is one that was created by first
clicking on a command button "Add new record". THIS is
the action that actually places the record into the table,
not the changes I was dumping when your code says "Undo".

I'll just rethink this and probably add a "Delete Record"
option if they need to get rid of the record.

Thank you!
 
It *cannot be done* with a commanfd button on the form, with 100%
reliability.

For example, say some field on the form is required. Go to that field, type
some characters, then erase them with spaces but leave the cursor in the
field. Now click your command button. Oops! The click event *won't fire*,
because the focus won't be able to leave the dirty, null, required field.
You'll have to press the Esc key once before the focus can leave that field.
In which case, you might as well press the Esc key *twice* to erase the
whole new record.

In summary, there is *no way* to code a 100%-reliable "cancel changes"
function using a normal command button on an Access form.

You can probably do it with a toolbar button, though.

HTH,
TC
 
Back
Top