manditory field

G

Guest

I need to make a field manditory on a form. I changed the SQL database to
not allow nulls and refreshed and relinked, I've even tried recreating the
query and form but the user still can exit the form without entering data.

This code asks the user to enter data but they can still close the form
without entering the data.

Any suggestion on making this a manditory field to close the form?

Private Sub cmdClose_Click()
On Error GoTo Err_cmdclose_Click

If IsNull(Me.ICNSR) Then
MsgBox ("Please enter a code.")
DoCmd.CancelEvent
End If

DoCmd.close

Exit_cmdclose_Click:
Exit Sub
 
D

Douglas J. Steele

If IsNull(Me.ICNSR) Then
MsgBox ("Please enter a code.")
DoCmd.CancelEvent
Else
DoCmd.Close
End If

Of course, this doesn't stop them from simply clicking on the X to close the
form. You might want to put code in the form's Unload event. If you set
Cancel = True, the form won't close

Private Sub Form_Unload(Cancel As Integer)

If IsNull(Me.ICNSR) Then
MsgBox ("Please enter a code.")
Cancel = True
End If

End Sub
 
G

Guest

Fantastic, that worked....

Douglas J. Steele said:
If IsNull(Me.ICNSR) Then
MsgBox ("Please enter a code.")
DoCmd.CancelEvent
Else
DoCmd.Close
End If

Of course, this doesn't stop them from simply clicking on the X to close the
form. You might want to put code in the form's Unload event. If you set
Cancel = True, the form won't close

Private Sub Form_Unload(Cancel As Integer)

If IsNull(Me.ICNSR) Then
MsgBox ("Please enter a code.")
Cancel = True
End If

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