Link to home
Start Free TrialLog in
Avatar of MICHAED
MICHAED

asked on

Error Handling Routine Not Working

Hi,

I have an error handling routine with a custom message that works for the most part, however, sometimes I receive the runtime error message rather than the custom message I created.  

Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 3101 Then
    MsgBox "You must input an Activity and Task", vbExclamation, "Data Input"
    Response = acDataErrContinue
    Else
If DataErr = 3314 Then
    MsgBox "You must input Hours and Volume", vbExclamation, "Data Input"
    Response = acDataErrContinue
    End If
End If
End Sub


The routine is within the "On Error" property of both a Main form as well as its Subform.  Is there something wrong with this routine?

Thanks,
Ed



Avatar of jadedata
jadedata
Flag of United States of America image

Hey MICHAED!

  You would be better served to pre-empt these errors at the source

  Build a function called
    Form_Valid() as Boolean

  Include code in this routine to test for the existance of the conditions currently be barked about in your form error trap.

  If the unacceptable conditions exist the function will report FALSE, if all data requirements are met the function returns TRUE (form_valid)

  Fire this testing function in places like the cmdClose button code to check the record, or make a button SPECIFICALLY to fire this function and test the record and report to the user the flaws.

  The function can be fired at any point the form is open to test validity and take the necessary action.

 
regards
Jack
Avatar of MICHAED
MICHAED

ASKER

Geez Jack, I'm keeping you busy today!  

I know the source of the errors, the are required fields within the underlying table.  I was testing the form to ensure that the user must populate all required fields within the subform, if not, one of the runtime errors appears.  I just wanted the message to be more user friendly via a customize message -- which I have accomplished.  However, sometimes the custom message does not appear, rathe, the runtime error appears in it's place.

So basically, I still get "runtime error 3314" and "runtime error 3101" even though I should be receiving the custom message in their place.

Ed
Avatar of MICHAED

ASKER

I guess what I'm saying is that I do not know how to implement what you are suggesting, so I am looking for a slightly easier solution -- even though it may not be the best. :)

Ed
SOLUTION
Avatar of morpheus30
morpheus30

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MICHAED

ASKER

Great help -- both of you.

Thanks!
Ed
Firstly replace your IF statements with a Select Case and secondly put in an error handler inside the code, finally do you have any code that is running that could also be triggering the runtime error, if so then add error handling to it.

Cheers, Andrew

On Error Goto Form_Error_Error
Select Case DataErr
    Case 3101
         MsgBox "You must input an Activity and Task", vbExclamation, "Data Input"
         Response = acDataErrContinue
    Case 3314
         MsgBox "You must input Hours and Volume", vbExclamation, "Data Input"
         Response = acDataErrContinue
End Select

Form_Error_Exit:
    Exit Sub

Form_Error_Error:
    Select Case Err
        Case Else
             MsgBox "An Unexpected error has ocourred in Form Error", vbCritical
             Resume Form_Error_Exit
    End Select
Thanx for another fine question!