Validating on close of a form (1 Viewer)

Y

Yvanne

Guest
I am trying to ensure the validation of fields on a form: the user may click a button to save the record or close the form to save the record. They need not "enter" the fields concerned, therefore the field validation does not work. I have managed to trap the errors if the save button is clicked, but on close of the form, I cannot seem to "back-out" of closing the form. Can anyone help?
 

The Real Yoda

Registered User.
Local time
Today, 23:41
Joined
May 25, 2001
Messages
17
Use the EXIT SUB commannd before the Docmd.close

e.g.

if x = true then
exitsub ' This stops the code if the if statement is met
Else

DoCmd.close
 

AlanS

Registered User.
Local time
Today, 18:41
Joined
Mar 23, 2001
Messages
292
Try this event procedure, which should be triggered whenever a record is updated, whether by closing the form, clicking a button, or however.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If (invalid_data_condition_1) then Cancel = True
If (invalid_data_condition_2) then Cancel = True
...
If (invalid_data_condition_n) then Cancel = True
End Sub
 
Y

Yvanne

Guest
I hasten to add that I forgot to say that the form also includes a subform and that the validations concerned are in the subform...Sorry!

In reply to The Real Yoda:
The DoCmd.Close before the ExitSub does not apply if the form is closed using the "X" icon on the form, I actually use your suggestion on the button to save the record and close the form already.

AlanS:
I have tried your suggestion previously but I think it is something to do with the subform since the error message pops up but the form still closes.

My problem only occurs when I choose the "X" icon to close the form.

Thanks for the suggestions so far!
 

Users who are viewing this thread

Top Bottom