Cancel New Record

WillC

Registered User.
Local time
Today, 03:15
Joined
Aug 18, 2011
Messages
17
I have a button that opens a form and goes to a new record and vba automatically fills in a textbox and a combo box. I have a canel button that contains
Form![FormName].undo
DoCmd.Close acForm, "FormName"

it deletes everything the user puts in himself but keeps the record with the fields vba filled in. I have looked at forums and it says to add cancel = true in beforeupdate event of form, however doesn't seem to be working, I even put a Msgbox textbox to test beforeupdate worked but it doesn't appear. I checked the table and it saves the new record as soon a vba fills it in. Any help appreciated thanks
 
Create a global variable (i.e. declare a Public variable in the declarations section of your form) and set it to True when the Cancel button is clicked.
Check against that variable in the Before Insert event of the form and set Cancel to True if that boolean variable is True.
 
Thanks I just tried that, but still didn't work. It's actually saving to table before cancel button has been clicked... or is will this remove it? any other suggestions
 
It depends on how you implemented my suggestion. Let me see your full code.
 
Option Compare Database
Public CancelPressed As Boolean


Private Sub Cancel_Click()
Cancel = True
Forms![Project Budget].Undo
DoCmd.Close acForm, "Project Budget"
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
If CancelPressed = True Then
Cancel = True
End If
End Sub

thanks for looking
 
I actually did say the Before Insert event, not the Before Update event. Plus, this is how your click button should be:
Code:
Private Sub Cancel_Click()
    CancelPressed = True
    DoCmd.Close acForm, "Project Budget"
End Sub
 
Oh Sorry so you did, I'll try that. I have undo command in there too as also use it for when a user changes a record and doesn't want to save changes, so redoes it to original before saving. Will this work for this case too? as have this code on all my forms. Thanks I appreciate it
 
The Undo command doesn't stop it from saving, it just undoes any edits. So that line isn't needed. You're closing the form and stopping any new inserts by setting Cancel = True.
 

Users who are viewing this thread

Back
Top Bottom