Requried Fields - Message Boxes

  • Thread starter Thread starter Steven M. Britton
  • Start date Start date
S

Steven M. Britton

I have four required fields on a form, if the user doesn't
enter data in one of those fields and attempts to save the
record they get a message that says "You can't go to
selected record".

How do I have a message box pop up saying WHICH required
field is missing...???

-Steven M. Britton
 
One approach is to use the BeforeUpdate event of the *form* to check if your
fields are null.

Example:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
Dim bWarn As Boolean
Dim strField As String

If IsNull(Me.Surname) then
strMsg = strMsg & "Surname required." & vbCrLf
strField = "Surname"
End If
If IsNull(Me.City) Then
strMsg = strMsg & "City required." & vbCrLf
strField = "City"
End If

'etc for other required fields.

If Cancel Then
strMsg = strMsg & "Enter the data, or press <Esc> to undo."
MsgBox strMsg,,"Invalid data"
Else

'Do your warnings.
If IsNull(Me.Address) Then
bWarn = True
strMsg = strMsg & "Address is blank." & vbCrLf
strField = "Address"
End If
If Me.BirthDate > Date Then
bWarn = True
strMsg = strMsg & "Born in the future?" & vbCrLf
strField = "BirthDate"
End If

'etc for other warnings.

If bWarn Then
strMsg = strMsg & vbCrLf & "Proceed anyway?"
If MsgBox(strMsg, VbYesNo+vbDefaultButton2, "Warning") <> vbYes
Then
Cancel = True
End If
End If
End If

'Set focus to the problem field.
If Cancel And Len(strField) > 0 Then
Me(strField).SetFocus
End If
End Sub
 
Use the form's "BeforeUpdate" Event and have it check each of the 4 required
fields. As it checks each of the fields, if it finds a field that's not
filled is as it's required, have it fill in a string type variable.

Once this check is complete, have it check the string variable and have a
message box pop up stating which fields (based on the string variable) is
not filled if, should the string variable not be empty. Just before popping
up the message box, be sure to have the Cancel variable changed to "True"
(without the quotes), so as the form doesn't attempt to update the record.

You could use other variable types for this information gathering as it
checks the required fields, rather than a string type variable, but the key
is that you need to do this in the form's "BeforeUpdate" Event, and should
at least one of the required fields not be filled in, you would set the
Cancel property to "True" (without the quotes), and have a messagebox pop up
stating why.

With my forms, I use labels on the forms as a status type deal and return
error messages and other things into it.
 
Allen, where's the Cancel being set to True prior to the:

If Cancel Then

The Cancel variable is "False" (without the quotes) or the value of 0 by
default. Therefore, when it comes to the statement:

If Cancel Then

It will always return False or the value of 0, unless this Cancel property
is set to True prior to this statement line.

I assume you meant to have this variable set to True in between each of the
sets of conditional checks between the lines of:

If IsNull...

and

End If
 
Thanks, Ronald.

Steven, it does need the line:
Cancel = True
in the If blocks for the required fields, e.g.:
If IsNull(Me.Surname) then
Cancel = True
strMsg = strMsg & "Surname required." & vbCrLf
strField = "Surname"
End If

--
 
Thanks, I am pretty new at this and I am not in the IT
field. Both of your explainations were excellent and
really helpful. Again I can't thank you enough I just
hope that all the users of this newsgroup get answers as
well phrased as yours.

-Steven M. Britton
Contracts Administrator
The Arc of San Diego
www.arc-sd.com
 
Back
Top