verify text boxes when users click close window button

G

Guest

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
 
G

Guest

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
 
K

Kevin K. Sullivan

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top