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. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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 | |
|
|