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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create true CANCEL button that deletes record 5

Status
Not open for further replies.

DH

Programmer
Dec 8, 2000
168
I have several forms that use a macro to open a form in add mode. I also have a CANCEL button on the form that is programmed to close with no save incase the user decides not to make an entry. However, when CANCEL is clicked the close and no save macro executes closing the form but a record is still created in the table. How can I prohibit a new record from being created???

Thanks!
 
Put the following code on your Cancel button:

----------
If Me.NewRecord Then
If MsgBox("Are you sure you wish to cancel record", 36, " Continue?") = vbYes Then
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
DoCmd.Close
End If
End If
----------

The DoMenuitem line will UNDO the record that was created when you opened the form in add mode, and then the form will close. Jim Lunde
compugeeks@hotmail.com
Custom Application Development
 
Thanks for the code. However, I must not be putting the code in the correct place. An error exists when I click the cancel button and I am not sure of its origin.

Perhaps I am just putting the code in the wrong place. What is the correct placement? Is anything else required?

Thanks again!
 
I don't know how familiar you are with VBA, but I will assume you are not, (forgive me if you are):

In design view of your form:

1) Name your cancel button "cmdCancel"
2) Right-click on the button, and select "Build Event"

You should see the following:

==========
Private Sub cmdCancel_Click()

End Sub
==========

3) Copy and paste the code above BETWEEN the Private and End Sub lines.
4) Close the form, and save it.
Jim Lunde
compugeeks@hotmail.com
Custom Application Development
 
You could run the undo from the menu OR merely use me Me.undo in the cancel button click event.
 
WARNING! I have had situations where invoking Undo like this undoes the changes to a previously saved record.

To see this:
1. Open any table or query, or any form over a table.
2. Modify a record and either save it, or move to the next record (causing it to be saved automatically).
3. Click Undo on the toolbar. The changes to the previous record are undone.

Undo performs 2 different functions. If a control has been modified, it restores the original value of the control. Otherwise, it undoes the changes to the most recently saved record.

If you want to use Undo this way, you probably want to condition it on Me.Dirty:
Code:
    Private Sub cmdCancel_Click
        If Me.Dirty Then Me.Undo
        DoCmd.Close
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top