How do I cancel out record entry through a form?

  • Thread starter Thread starter Chrisso
  • Start date Start date
C

Chrisso

Hi All

I have a form that adds records to a table. On Form_Load I run the
following code to make sure that the user is always entering a new
record at the end:

DoCmd.GoToRecord , , acNewRec

On my form I have an "OK" and a "Cancel" button. If the user is happy
with all the data they enter then they click "OK" and the data is
added as a new record - no problem.

I also want to be able to cancel the record altogether if they hit
"Cancel". How do I achieve this? In this case I want to back out the
data record the user was working on before they clicked "Cancel".

Thanks in advance,
Chrisso
 
Try this code in the Click event procedure of your cancel command button
(named cmdCancel in this example):

Private Sub cmdCancel_Click()
If Me.Dirty Then Me.Undo
DoCmd.Close acForm, Me.Name
End Sub

That will work unless:
a) The record has already been saved, or
b) The user can't get to the button because the current field has an
incomplete value (e.g. they typed text into a Number field.)
 
hi Chrisso,
I also want to be able to cancel the record altogether if they hit
"Cancel". How do I achieve this? In this case I want to back out the
data record the user was working on before they clicked "Cancel".
Just a simple

If Me.Dirty Then
Me.Undo
End If

btw, instead of using the

DoCmd.GoToRecord , , acNewRec

in the Form_Load event I would use the acFormAdd parameter for
DoCmd.OpenForm.


mfG
--> stefan <--
 
Thanks Allen and Stefan.

Exactly what I needed.

For others: Stefan suggested supplying the DataMode parameter:

DoCmd.OpenForm "frmAddRecording", DataMode:=acFormAdd

which works a treat.

Chrisso
 
Back
Top