|
Question : Close form in access and abandon half fillled record - without any access messages/Have a button/event do the same thing as the escape key.
|
|
I have a data entry form in Access 2003. If the user has got part of the way through adding a new customer record and instead of pressing the Exc button simply clicks the X to close the window - I want the form to just close without any warnings - just abandon the record half done and close.
I don't want to have to add my own close button. I have tried various bit s of code in the On Close and On Unload event including docmd. close and various runcommands that look like they might do something (!) but these (or at least the dozen or so I tried!) won't work. The docmd.close just results in the run time error 2501 "The close action was cancelled" .
If there was a way on doing the same thing as the Esc key on the keyboard just before the close this might help avoid the errors in the first place - but I can't find a way of doing this. My escape comand button I created does a cancelevent - but (if you clcik in certain fields) this ends up with a customer record number being assigned in the customer ID autonumber field - so then you can't close.....
The command button for the esc does have the cancel property set to Yes.
There must be a way to do this?
Thnaks in advance
Lou
|
|
Answer : Close form in access and abandon half fillled record - without any access messages/Have a button/event do the same thing as the escape key.
|
|
Louverril,
> ... just to stop all these confusing access messages coming up...
From your description, you do the data validation in the code of the Save button. This is not the best way.
The form's Before Update event is meant for that. It will run whichever way the user chooses to attempt a save (your button, closing the form, surfing to a new record, pressing Shift+Enter, etc.) Let's take the simple case where LastName is required.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.txtLastName) Then Me.txtLastName.SetFocus MsgBox "The Last Name is a required field" Cancel = True End If
End Sub
Private Sub cmdSave_Click() Me.Dirty = False End Sub
You can make this more elaborate, for example with the message box offering a way out (e.g. with OK and Cancel buttons). You can use Me.Undo in the Before Update event in conjunction with Cancel=True. This will work even if the form is in fact being closed.
Finally, to catch the "can't save at this time" message, you need a Form_Error handler. This will catch all errors not launched from one of your buttons, but from other user interactions like closing the form or pressing the record selector (which also saves the record).
Anyway, you need to think "events" and not "actions". You cannot correctly handle validation in any other event than Before Update.
As for you main request, have you tried creating a small form with just my two functions? It takes about 3 minutes...
Cheers! (°v°)
|
|
|
|