A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Force Validation prior to leaving form.



 
 
Thread Tools Display Modes
  #1  
Old October 29th, 2008, 06:40 PM posted to microsoft.public.access
Bunky
external usenet poster
 
Posts: 374
Default Force Validation prior to leaving form.

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.
  #2  
Old October 29th, 2008, 07: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.


  #3  
Old October 29th, 2008, 07:59 PM posted to microsoft.public.access
Bunky
external usenet poster
 
Posts: 374
Default Force Validation prior to leaving form.

THANK YOU!! That makes sense.
Kent

"BruceM" wrote:

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.



  #4  
Old October 29th, 2008, 09:01 PM posted to microsoft.public.access
BruceM[_2_]
external usenet poster
 
Posts: 1,763
Default Force Validation prior to leaving form.

Glad to help.

"Bunky" wrote in message
...
THANK YOU!! That makes sense.
Kent

"BruceM" wrote:

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.




 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 10:05 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.