How to generate a user-friendly message for checking required fields?

  • Thread starter Thread starter Amit
  • Start date Start date
A

Amit

MS Access 2K, Windows XP
=========================
Hi,

In my Access application, I have forms where some of the
controls are required fields. When a user doesn't enter a
required field, the error message generated is too
technical for the users.

How do I check that:
1. all required fields have been filled in,
2. suppress the error message,
3. and display my own message with proper information?

Where will I place this code?

I also have multiple tab pages on a form, with required
fields on one tab page. So, I'll need to check for
required fields not only when a user closes the form, or
moves to a new record on the same form, but alo when she
moves to the next tab page on the same form.

Thanks for any help.

-Amit
 
There are actually 2 events you need to use to achieve this.

1. The Error event of the form will fire if the person begins an entry in a
required field, and erases it. Trap the DataErr, display your own message,
and then set Response to acDataErrContinue.

2. Use the BeforeUpdate event of the *form* to check if each field IsNull().

This example shows how to check multiple fields in Form_BeforeUpdate, and
report a single message:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String

If IsNull(Me.SomeField) Then
Cancel = True
strMsg = strMsg & "Somefield required." & vbCrLf
End If

If IsNull(Me.AnotherField) Then
Cancel = True
strMsg = strMsg & "AnotherField required." & vbCrLf
End If

'etc for other fields.

If Cancel Then
strMsg = strMsg & vbCrLf & "Complete the record, or press <Esc>
twice to undo."
MsgBox strMsg, vbExclamation, "Invalid record"
End If
End Sub
 
-----Original Message-----
There are actually 2 events you need to use to achieve this.

1. The Error event of the form will fire if the person begins an entry in a
required field, and erases it. Trap the DataErr, display your own message,
and then set Response to acDataErrContinue.

Hi Allen,

I'm soemwhat lost here regarding 1. I understand why I
need to trap error in the On Error event, but not sure how
to implement it. Could you possibly give me an example of
how to trap the DataErr?

Thanks for your prompt response.

-Amit
 
1. Open your form in design view.
2. Open the Properties box.
3. On the Event tab (looking at the properties of the form), see the Error
event.

The Error event fires when there is an engine level error, not a VBA error.
 
Back
Top