Solved

Error Handling Routine Not Working

Posted on 2003-11-18
8
713 Views
Last Modified: 2009-12-16
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



0
Comment
Question by:MICHAED
8 Comments
 
LVL 32

Expert Comment

by:jadedata
ID: 9773626
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
0
 

Author Comment

by:MICHAED
ID: 9773698
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
0
 

Author Comment

by:MICHAED
ID: 9773771
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
0
 
LVL 5

Assisted Solution

by:morpheus30
morpheus30 earned 75 total points
ID: 9773816
This shouldn't really make a difference, but it might help you with the errors later...

Private Sub Form_Error(DataErr As Integer, Response As Integer)

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
CASE ELSE
    MsgBox Err.Number & ": " & Err.Description
    Response = acDataErrContinue
END SELECT

End Sub
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 32

Accepted Solution

by:
jadedata earned 125 total points
ID: 9773832
Using the method you are, you have to account for every possible error that might arise.
Can you tell me now what errors will occur on this formset next month???

Instead catch these before they become errors...as in this minor example...

Private Function Form_Valid() as Boolean

   If Isnull(me("txtFieldName")) then
     msgbox "FILL IN THE FREEKIN' FIELDS, WILL YA!!!,vbokonly+vbcritical,"User Flamed"
     Form_Valid=False
     exit function
   endif

   'if the user makes it to here, all is well
   Form_Valid = True

End Function

If you have a Close button on your form that YOU implemented (turn off the window controls top right of form window)

  Private Sub cmdClose_Click()
    if not Form_Valid() then
       msgbox "Correct the data issues before leaving..."
    endif

    docmd.close acform, me.name

  end sub

 
0
 

Author Comment

by:MICHAED
ID: 9773876
Great help -- both of you.

Thanks!
Ed
0
 
LVL 28

Expert Comment

by:TextReport
ID: 9773885
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
0
 
LVL 32

Expert Comment

by:jadedata
ID: 9773909
Thanx for another fine question!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now