Originally Posted by
Khalil Handal
Hi,
I have eleven required fields on a form. I am trying to replace the default error message generated by access. I am trying to use the Form's Error() Event.
I found examples for several cases; like four different errors for four different fields, each field has its different error message.
I could not find example of the same error 3314 (field is required) for different fields. Is it possible to have a different error message for each field ?
Khalil
It appears you are trying to do data validation, making sure that each of the 11 required fields have been filled in.
What do you mean by "A different error message for each field" Do you want the error message to identify the offending field?
One way to validate multiple fields is to use the controls tag property and loop through the form controls to check for missing data.
Normally this is done in the before update event or before a procedure is run.
I prefer one message box to include all missing fields and also changing the control bordercolor to red to highlight it.
I use the following function which returns true if fields are empty
Code:
Public Function ValidateForm(frm As Form, TagCharacter As String) As Boolean
'validated controls must have a label. Ok to use a hidden label if needed.
'Returns True if missing Data
Dim ctl As Control
Dim flg As Boolean
Dim strOut As String
flg = False
For Each ctl In frm.Controls
If ctl.Tag = TagCharacter Then
If Nz(ctl.value, "") = "" Then
flg = True
ctl.BorderColor = vbRed
strOut = strOut & Space(10) & "* " & ctl.Controls.Item(0).Caption & vbNewLine
Else
ctl.BorderColor = vbBlack
End If
End If
Next
If flg = True Then
MsgBox "The following field(s) must be completed:" & vbNewLine & strOut
End If
ValidateForm = flg
End Function
If this helped, please click the star * at the bottom left and add to my reputation- Thanks