Form Validation

  • Thread starter Thread starter Joe Williams
  • Start date Start date
J

Joe Williams

I have an app linked to SQL server and whenever a user fails to put in a
required field you get a really ugly and downright unintelligble error
message telling you that you forgot to fill in a required field.

I would like to build in some logic to the form to do some of this
validation before I get ugly messages from SQL server.

What is the best place to call this function (IE user hits save, each time
you lose focus on a field, etc)

ALSO, how can I have a function that runs a number of validation checks and
presents the user with a message box listing all of the things wrong? I know
how to doa single line message box, but not sure about how to have the
function check a number of fields and do a multi-line error message to the
user, such as this:

Date is missing and is a required field
Employee Id is missing and is a required field
Quantity must be greater than zero

All three would be in one message box to the user.

Any help?

Thanks

joe
 
Hi:

Try using the Form_Error event.

Here DataErr is err number to trap and you can replace Access error message
with yours and then set Response to acDataErrContinue which prevents
Access's default error message from popping up.


Private Sub Form_Error(DataErr As Integer, Response As Integer)
Const conDuplicateKey = 3022
Dim strMsg As String

If DataErr = conDuplicateKey Then
Response = acDataErrContinue
strMsg = "Each employee record must have a unique " _
& "employee ID number. Please recheck your data."
MsgBox strMsg
End If
End Sub

Regards,

Naresh Nichani
Microsoft Access MVP
 
Back
Top