×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Create true CANCEL button that deletes record
5

Create true CANCEL button that deletes record

Create true CANCEL button that deletes record

(OP)
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!

RE: Create true CANCEL button that deletes record

3
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

RE: Create true CANCEL button that deletes record

(OP)
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!

RE: Create true CANCEL button that deletes record

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

RE: Create true CANCEL button that deletes record

You could run the undo from the menu OR merely use me Me.undo in the cancel button click event.

RE: Create true CANCEL button that deletes record

2
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:

    Private Sub cmdCancel_Click
        If Me.Dirty Then Me.Undo
        DoCmd.Close
    End Sub

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close