View Single Post
  #2  
Old October 29th, 2008, 06:42 PM posted to microsoft.public.access
BruceM[_2_]
external usenet poster
 
Posts: 1,763
Default Force Validation prior to leaving form.

You need to cancel the update.

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(Me.User) Then
MsgBox "Please Enter a Specialist."
Me.txtUser.SetFocus
Cancel = True
End If

If IsNull(Me.AttendDate) Then
MsgBox "Please Enter a valid Date."
Me.txtAttendDate.SetFocus
Cancel = True
End If

If IsNull(Me.Comments) Then
MsgBox "Please Enter Comments."
Me.txtComments.SetFocus
Cancel = True
End If

End Sub

Note that I added Me. before the field names. I don't know for sure if that
is necessary, but there it is. If nothing else it uses the VBA editor's
Intellisense capability to provide you with a list of fields after you type
Me. Also, I set the focus to the text box that needs an entry.

"Bunky" wrote in message
...
I have a form that I am trying to validate some data and I do not want to
have the focus change to a new record until the old record is correct.
Initially, I had all the validation done on when the close button was
clicked
and this worked fine. However, the operators can use the navigation
arrows
to go to a new record as well which, of course, bypasses all my code. I
then
put the validation code in the form on the Before Update event. This
works
but after I get the message box to display and click ok, it moves to a new
record rather than go back and validate again. Here is my code.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(User) Then

MsgBox "Please Enter a Specialist."
End If

If IsNull(AttendDate) Then

MsgBox "Please Enter a valid Date."
End If

If IsNull(Comments) Then

MsgBox "Please Enter Comments."
End If

End Sub

How do I make the code stay in this routine until all data has been
entered?
Obviously, I am not a VB coder and am picking this up OTJ. Any help is
appreciated.