<bu*****@yahoo.com> wrote :
I have 2 fields for an end-user that must be filled in. I want an error
message for each field.
I've provided two options which work for me rather well; hopefully you can
make use of one them.
Cheers,
Dave
Option One
'------------ Code Begins ------------
Private Sub Form_BeforeUpdate(Cancel As Integer)
' Ensure required fields are filled.
' Required fields, which are empty: show a customized message box to that
effect,
' and return the focus for user input.
' Check that a Category has been chosen before proceeding.
If IsNull(Me.cboCategories) Or Me.cboCategories = 0 Then
Call MsgBox("You must select a Category from the list provided, or " _
& vbCrLf & "enter a new category using the ""Edit Categories"" button
on the main menu!" _
, vbExclamation, "ENTRY REQUIRED")
Me.cboCategories.SetFocus
Exit Sub
End If
' Check that a Sub-Category has been chosen before proceeding.
If IsNull(Me.cboSubCategories) Or Me.cboSubCategories = 0 Then
Call MsgBox("You must select a Sub-Category, or choose Null from the
list provided." _
& vbCrLf & "" _
& vbCrLf & "If ''Null'' or a desired value is not available from the
list provided, you " _
& vbCrLf & "can create a new one by using the ''Edit Categories''
button, and " _
& vbCrLf & "add a new sub-category to a main category heading." _
& vbCrLf & "" _
, vbExclamation, "ENTRY REQUIRED")
Me.cboSubCategories.SetFocus
Exit Sub
End If
' Check to ensure a Question or Title has been entered prior to proceeding.
If IsNull(Me.txtQuestion) Or Me.txtQuestion = "" Then
Call MsgBox("Question is a required field!" _
& vbCrLf & "" _
& vbCrLf & "Please enter a question or title for future reference" _
& vbCrLf & "" _
, vbExclamation, "ENTRY REQUIRED")
Me.txtQuestion.SetFocus
Exit Sub
End If
End Sub
'------------ Code Ends ------------
Option Two (Note, this option does not return the user to a specific
field...modify as your see fit)
'------------ Code Begins ------------
Private Sub Form_BeforeUpdate(pintCancel As Integer)
' Check for Null values, and get confirmation each time a record is changed.
Dim strMissing As String
Dim strCRLF As String
' line feed
strCRLF = Chr(10) & Chr(13)
' 1st field value is null?
strMissing = IIf(IsNull(Me.txtSurname), "- Surname" + strCRLF, "")
' 2nd field value is null?
strMissing = strMissing & IIf(IsNull(Me.txtGivenName), "- Given Name" +
strCRLF, "")
' 3rd field value is null?
strMissing = strMissing & IIf(IsNull(Me.cboGroup), "- Group" + strCRLF,
"")
' 4th field value is null?
strMissing = strMissing & IIf(IsNull(Me.cboGrpFilter), "- Group Filter"
+ strCRLF, "")
' 5th field value is null?
strMissing = strMissing & IIf(IsNull(Me.cboElement), "- Element" +
strCRLF, "")
' 6th field value is null?
strMissing = strMissing & IIf(IsNull(Me.txtSortNo), "- Sort Number" +
strCRLF, "")
' Any required field was null?
If strMissing <> "" Then
' No update
Cancel = True
' ask user is (s)he wants to enter data or cancel and close form
If MsgBox( _
"Required information is missing: " & strCRLF & _
strCRLF & _
strMissing & _
strCRLF & _
"Do you want to enter the missing data?", vbYesNo, _
"Data missing") = vbNo Then
Me.Undo
End If
End If
End Sub
'------------ Code Ends ------------