I have a form that people use and sometimes they click the x in the upper
right hand corner of the form to close it. I want to validate that certain
text boxes are not blank and if they are have a message box pop up and the
close action stopped.
I've been trying to code this and nothing seems to work.
what code should i use and what event should i put it in?
thanks
BruceS - 02 Feb 2006 04:51 GMT
Billy,
Set the form's "CloseButton" property to "No". This deactivates the "x" and
makes them use your button to close the form.
Note that if they are allowed to close the form using Access controls in the
menu bar or by right-clicking, your "Close Button" logic will still not be
processed.
Bruce
> I have a form that people use and sometimes they click the x in the upper
> right hand corner of the form to close it. I want to validate that certain
[quoted text clipped - 6 lines]
>
> thanks
Kevin K. Sullivan - 02 Feb 2006 15:54 GMT
BillyRogers,
Option one is to use data validation rules in your table (strongest) or
in your form. Another option, especially if the validation is
complicated and depends on the values of multiple fields is to work with
the Form's Unload event. If your validation fails, set the Cancel
parameter to True and the form won't close. This event fires whether
you close via the Close button, DoCmd.Close, closing Access, etc.
air code sample
Private Sub Form_Unload(Cancel as Integer)
'validate form before closing is allowed
If Len(txtNonBlank & "") = 0 then
Cancel = True
MsgBox "You must enter a value for the field NonBlank.", vbOKOnly
txtNonBlank.SetFocus
Exit Sub
End if
If Len(txtNeedsText & "") = 0 then
Cancel = True
MsgBox "You must enter a value for the field NeedsText.", vbOKOnly
txtNeedsText.SetFocus
Exit Sub
End if
If txtMinimum >= txtMaximum then
Cancel = True
MsgBox "Minimum must be less than Maxiumum", vbOKOnly
txtMinimum.SetFocus
Exit Sub
End Sub
---------
HTH,
Kevin
> I have a form that people use and sometimes they click the x in the upper
> right hand corner of the form to close it. I want to validate that certain
[quoted text clipped - 6 lines]
>
> thanks