Help with validation

belsha

Registered User.
Local time
Today, 06:17
Joined
Jul 5, 2002
Messages
115
I am using the code below, and need it to do two additional things -
1) Stop at each if block and not allow the user to go on until the missing piece of information is filled in, then go on to check the other areas. As is the user gets the messagebox, but as soon as they click OK and before they fill in anything, the Check Completed, OK to Print box pops up. 2) After the routine has completed and all the information is complete, save the record. Any suggestions would be appreciated.

Private Sub cmdCheck_Click()
If IsNull (Me.RevDate) or Me.RevDate = "" Then
MsgBox "You must enter a review date", vbOKOnly, "Required Data"
Me.RevDate.SetFocus
End If

If IsNull (Me.ProvCd) or Me.ProvCd = "" Then
MsgBox "You must enter a provider code", vbOKOnly, "Required Data"
Me.RevDate.SetFocus
End If

MsgBox "Check completed, ready to print."

cmdPrintCover.enabled = True

End Sub
 
A simple way is to exit the sub if an error condition is found:
Code:
Private Sub cmdCheck_Click()
If IsNull (Me.RevDate) or Me.RevDate = "" Then
    MsgBox "You must enter a review date", vbOKOnly, "Required Data"
    Me.RevDate.SetFocus
    Exit Sub
End If

If IsNull (Me.ProvCd) or Me.ProvCd = "" Then
    MsgBox "You must enter a provider code", vbOKOnly, "Required Data"
    Me.RevDate.SetFocus
    Exit Sub
End If

MsgBox "Check completed, ready to print."

cmdPrintCover.enabled = True

End Sub
 
Tried that, it takes the user there but still allows them to leave the field without entering anything, which is a problem. Also, if they do fill in the field, they have to reclick the check button for them to get the check completed, OK to print message. I want it to check each field (there are many more than the two I listed), stop and set focus at each one that needs something filled in (and not let them leave until something is filled in), and go on to the next field to check. At the end when all the fields have been checked and filled in, Save the record (with all the fields filled in, of course), and enable the printcover button. Thanks!
 
Then it's best to have code in the On Exit event of each field that requires input. Check for a valid entry before letting the user proceed.

I would point the On Exit code of each field to an error checking routine that checks all the fields in turn. If it passes the error check, then I would enable the cmdPrintCover button.
 
Use the BeforeUpdate event of the form, where you can prevent the button from working and or cancel the update
 
dcx - There are cases when the user will have to go back and look up a piece of information, and I don't want to lock them into not being able to enter past that piece if they don't have it, only not allow them to print until the whole form is filled in, thus the check button which they hit before printing. Also, how or where would I put the save command, I tried DoCmd.Save after the print button was enabled, but got an error message.
 
Pat - the save portion now works appropriately. Can you or anyone else give a suggestion for the code that will check all the fields at the end (when the person clicks the check button, which they should do before printing), go thru each field to see that it is filled in and notify if it is not, then go on to the next field and do the same. For reasons stated before, I do not want to use field validation, only validation that runs the whole routine when the user clicks the button for the check. Thanks for all the help.
 

Users who are viewing this thread

Back
Top Bottom