Trying to create a cancel button.

  • Thread starter Thread starter JFELLA
  • Start date Start date
J

JFELLA

I'm new to access and know very little about code, In a form I created, I want
a cancel button. I used the command button wizard to make an undo button, but
it comes up with an error message if nothing is typed. Also I would like it to
close the form when clicked.

Thanks in advance.
 
In VBA, add following to the Cancel button's click event code
as first line:

If Not Me.Dirty Then Exit Sub

As to closing the form:
Doesn't the wizard also present a way to close a form?
Have a closer look in this wizard; there shud be an option
to create a formclose button off the wizard.

Krgrds,
Perry
 
The problem with your button is that Access must process the field the
cursor is in *before* it can move focus to the button and process its Click
event. That means if the current field does not have a satisfactory value,
the button cannot "undo" it.

One solution is to put the button somewhere else, such as on a toolbar.
There is a built-in Undo button on the form toolbar that works very nicely
and requires no code.

A better solution is to train your users to press <Esc> to undo the entry.
Pressing the <Esc> key a second time undoes all the fields of the current
record in the form.

If you want a command button that throws away any uncommmitted edits and
closes the form, and you are happy with the limitations in paragraph 1
above, use this code in the Event Procedure of your button's Click event:

Private Sub cmdCancel_Click()
If Me.Dirty Then
Me.Undo
End If
DoCmd.Close acForm, Me.Name
End Sub
 
Oh for the Oracle feature: "process this button's Click event >without<
trying to take the focus" :-)

TC
 
The way I did it...create a macro to first--close the
form, then open it back up using a query to display the
last record entered...then you can use an id field in the
table as a source to delete the displayed entry using a
delete query...takes a little work but was the only thing
that worked for me because you can't delete the record
until it is saved and you can't close the form without
saving the record.
 
Back
Top