Cancel = True, Still get the Save Error

stevenblanc

Registered User.
Local time
Today, 07:19
Joined
Jun 27, 2011
Messages
103
Alright folks,

It's been a while, I hope everyone is well. This is a stupid error that I am hoping is due to some small oversight. I've simplified the process just to minimize that chance.

Here goes:

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    Cancel = True
End Sub

I have several required fields which if the user decides to hit cancel or the red x close button I wish to simply cancel the save. I had the Cancel button working, however for the life of me i could not get the form close button to bypass the save attempt. I eventually reduced the code to its present form above to see if that would do the trick. No dice.

I also attempted placing "Cancel = True" in the form_load event to no avail. How can I cancel users entry if they hit the red x close button? I tried looking on the forums but nothing yet. If it matters, I'm using Access 2010.

Cheers,


Steven
 
Undo what you've done using the form's Undo method.
 
Nothing has been done. I'm talking I open the form and attempt to close it immediately.

EDIT: Works all the same... Thanks a million, it was driving me crazy.
 
Is the Data Entry property set to Yes?
And are you sure you're not setting some controls via code?
 
Yea that was the problem, not code per se, but I had a default date value.
 
I did. The current code is now:

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.Form.Undo
Cancel = True
End Sub

and the error is:

You can't save this record at this time.

Microsoft Access may have encountered an error while trying to save a record.
If you close this object now, the data changes you made will be lost.
Do you want to close the database object anyway?

Error Handling doesn't pick it up and I still get the error. I tried sticking a message box into the before_update, and I get the above error before the message box pops up.
 
I am contemplating making all controls unbound, and passing the entry through when the save button is clicked. It will remove all of this hassle, if creating some extra work that should be automated. I suppose as a benefit I will stop losing autonumbers when users cancel forms.

Or I can remove all required fields and manually do the requirement evaluation.

But both methods mean admitting defeat to something so trivial...
 
Okay, so in attempting to recreate the problem on a blank form, I realized there was a number control being set via code and that's where the problem lies.

Will I need to manually set all controls to "' or NULL?
 
If it's the Close X button you're trying to trap then you need to treat this differently. Do you have a button you use for explicitly saving records? And do you a custom navigation menu?
 
I removed all navigation for entry. But I do have a button used explicitly for save and cancel. I can get both of those to work without any problems. It's just the Close X button that's giving me the trouble.
 
In liu of attaching a database this example demonstrates the problem:

Table - tblTest
Field: ID - Default Settings
Field: testText - Text, Required
Field: testDate - Date, Required
Field: testNum - Number, Not Required

Form - frmTest
Place the above three fields using default access names

Only Code required is
Code:
Private Sub Form_Load()
    testNum = 3
End Sub

Click X Close. If we can solve that, we're good to go. The requirement field can be handled by Before_Update with Cancel = True.
 
Here's some code to use:
Code:
Private blIsUnloading As Boolean


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

Private Sub Form_Error(DataErr As Integer, Response As Integer)
    If DataErr = 2169 Then
        Response = acDataErrContinue
    End If
End Sub

Private Sub Form_Load()
    blIsUnloading = True
End Sub
In the click event of your Save button set blIsUnloading to False.
 
Perfect. Thats basically the setup I had before. It was the form error handling that was pure magic.

Cheers!


Steven
 

Users who are viewing this thread

Back
Top Bottom