Trigger all validation rules before submitting a form

thraling

Registered User.
Local time
Today, 10:54
Joined
Mar 17, 2016
Messages
10
Hello,

I have a form with several fields(/combox) and cross-field validation rules.

My problem is that some of those fields may end up having a value (usually the default one) that is inconsistent with their validation rule but, not being touched by the user, they never trigger the rule validation check.

Is there a way to trigger all the validation rules before inserting/modifying the record?
 
I do my checks via code (not in the field property)
Easier to change...

usage:
if IsValidForm() then docmd.openquery "qaAddNewRecord"

Code:
Private Function IsValidForm() As Boolean
Dim vMsg
Select Case True
   Case IsNull(dtpWeekOf)
      vMsg = "Date field missing"
   Case IsNull(cboUser)
      vMsg = "Teacher name is missing"
   Case IsNull(cboSubj)
      vMsg = "Subject field is missing"
End Select
If vMsg <> "" Then MsgBox vMsg, vbCritical, "Required Field"
IsValidForm = vMsg = ""
End Function
 
I do my checks via code (not in the field property)
Easier to change...

usage:
if IsValidForm() then docmd.openquery "qaAddNewRecord"

Code:
Private Function IsValidForm() As Boolean
Dim vMsg
Select Case True
   Case IsNull(dtpWeekOf)
      vMsg = "Date field missing"
   Case IsNull(cboUser)
      vMsg = "Teacher name is missing"
   Case IsNull(cboSubj)
      vMsg = "Subject field is missing"
End Select
If vMsg <> "" Then MsgBox vMsg, vbCritical, "Required Field"
IsValidForm = vMsg = ""
End Function

Yes, thank you, I will as well work with beforeUpdate, basically re-writing all the rules (now their opposites for blocking possible errors) in VBA.

At this point I could have as well avoided to lose time writing the rules... Thought there was a faster and easier solution.

Thank you
 
Most validation belongs in the FORM's BeforeUpdate event. That way it fires regardless of whether or not the control ever got the focus. It is especially important when there are dependencies such as if fldA is present then fieldB must be empty or DateA must be <= DateB.
The only time I ever put validation in individual control Events is if I want to display an immediate error rather than allow the user to continue or if I need to show/hide additional controls. In those cases parts of the validation usually still end up in the Form's BeforeUpdate event.

Remember, the FORM's BeforeUpdate event is the LAST event to run before the data is actually committed. It is your chance to keep bad data from being saved. All you have to do is to set cancel to true. Each validation section that raises an error includes four statements:
Code:
        Msgbox "some meaningful message.", vbOKOnly
            Me.somecontrol.SetFocus
            Cancel = True
            Exit Sub
 

Users who are viewing this thread

Back
Top Bottom