Don't close form on error

  • Thread starter Thread starter sharontodd
  • Start date Start date
S

sharontodd

I want to display a custom message box for a form for when a required field
is left blank and the user attempts to close the form. I want the message
box to show then return to the active form to allow user to enter the needed
value. What conditions do I put for the code and under what event. Also,
how do I keep the form from closing after showing the message box?

Thanks !!

Sharontodd
 
You can use the form's Before Update event to check for required fields.
Cancel the Before Update event if the condition is not met. This sample
code checks the text box txtSomeField, which is bound to the field
SomeField:

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull (Me.txtSomeField) Then
MsgBox "You need to enter a value"
Me.txtSomeField.SetFocus
Cancel = True

End Sub

Use your actual control name in place of txtSomeField, and customize the
message as you wish. SetFocus is optional, but helpful for the user.
 
Please post the code you are using.

If a command button is used to close the form, try putting this into the
Click event before the code to close the form:
Me.Dirty = False
 
Use the code from BruceM but put it in the form's On Unload event. You can
stop a form from closing even if user clicks on Close Box ("x")
 
Back
Top