Here's another thought. I tend to separate all pre-update validation into a separate procedure. Here's an example:
Code:
Private Function Valid() As Boolean
On Error GoTo Valid_Error
Valid = False
If Len(Trim(Nz(Me.cboManufacturer, ""))) <= 0 Then
MsgBox "Please select or enter a manufacturer.", vbCritical + vbOKOnly, Me.Caption
Me.cboManufacturer.SetFocus
GoTo Exit_Procedure
ElseIf IsNull(Me.txtEngineType) Then
MsgBox "Please select an engine type.", vbCritical + vbOKOnly, Me.Caption
Me.txtEngineType.SetFocus
GoTo Exit_Procedure
ElseIf mfrmDatePrompt.PeriodStatus <> "Open" Then
MsgBox "Please select an acquisition date in an open period.", vbCritical + vbOKOnly, Me.Caption
Me.sfrDatePrompt.SetFocus
GoTo Exit_Procedure
ElseIf Me.txtCost <= 0 Then
MsgBox "Please enter a cost greater than zero.", vbCritical + vbOKOnly, Me.Caption
Me.txtCost.SetFocus
GoTo Exit_Procedure
ElseIf Me.txtDepreciation <= 0 Or Me.txtDepreciation >= 0.25 Then
MsgBox "Please enter a depreciation rate greater than zero and less than 25%.", vbCritical + vbOKOnly, Me.Caption
Me.txtDepreciation.SetFocus
GoTo Exit_Procedure
ElseIf Not IsNull(Me.txtDateLastLTO) Then
If Me.txtDateLastLTO > Date Or Me.txtDateLastLTO < DateAdd("yyyy", -2, Date) Then
MsgBox "Please enter a registration date within the last two years.", vbCritical + vbOKOnly, Me.Caption
Me.txtDateLastLTO.SetFocus
GoTo Exit_Procedure
End If
ElseIf Not IsNull(Me.txtDateLastEmission) Then
If Me.txtDateLastEmission > Date Or Me.txtDateLastEmission < DateAdd("yyyy", -2, Date) Then
MsgBox "Please enter an emission test date within the last two years.", vbCritical + vbOKOnly, Me.Caption
Me.txtDateLastEmission.SetFocus
GoTo Exit_Procedure
End If
End If
Valid = True
Exit_Procedure:
On Error GoTo 0
Exit Function
Valid_Error:
Err.Source = "Form_frm06NewTricycle"
ErrorManager.HandleError "Valid"
Resume Exit_Procedure
Resume
End Function
(Yes, I know a length cannot be less than zero - it's just a belt and braces thing.)
Then all I have to do in the form's Before Update event is code:
Code:
If Not Me.Valid Then Cancel = True
Personally I find this elegant and easier to debug.
PS Ha! Just seen a bug - and this has been around for more than three years! The final two 'ElseIfs' should be separate If - End If constructs.