Using a module to trap errors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hyperpau
    Recognized Expert New Member
    • Jun 2007
    • 184

    Using a module to trap errors

    Hi guys, I am wondering it this is possible.

    Basically, what I do currently is use the Form_Error() event to trap
    erros and display customer error messages.

    Problem is, I need to do this in every form. Of course, i know this is
    essential. But wouldn't there be a way to use a module to call during the Form_Error Event?

    Example: Instead of doing:
    Code:
    Public Sub Form_Error(DataErr as Integer, Response as Integer)
    If DataErr = 3022 
         'some message
    Else
        'some message
    End If
    End Sub
    I would then create a module with this sub:
    Code:
    Sub ErrorHandler()
    If DataErr = 3022 
         'some message
    Else
        'some message
    End If
    End Sub
    Then on the form's error event, do this.
    Code:
    Private Sub Form_Error(DataErr as Integer, Response as Integer)
    Call ErrorHandler
    Response = DataErrContinue
    End Sub
    This works when I tried it, the problem is only the Else part works if I put it on the module. It doesn't seem to recognize the DataErr: 3022

    Not to mention, when i use the Err.Number always just gives me 0

    Thanks in advanced.
  • puppydogbuddy
    Recognized Expert Top Contributor
    • May 2007
    • 1923

    #2
    see these links for sample global error handler vs procedural error handler:



    How to trap and log the errors reported while a Microsoft Access database is in use. (Access 95 and onwards.)


    If you agree that procedural error handling is better,safer and more secure.....but wish there is a way to reduce the work effort involved, see this link for a third party tool costing less than $30.

    Comment

    • hyperpau
      Recognized Expert New Member
      • Jun 2007
      • 184

      #3
      Thank you puppydogbuddy. But I have seen this links already before I posted.
      This does not solve my problem.

      Both pages still talks about calling the procedure within EVERY ErrorHandler of every sub in the form module.
      The 2nd link doesn't even talk about modules but local error handling.
      The 1st link uses a standard module function, however, I would still
      need to call the function from the ErrorHandler of every sub.

      Code:
      On Error GoTo ErrorHandler
      
      'some codes
      
      Exit_Error:
      Exit Sub
      
      ErrorHandler:
      Call "function name from the standard module"
      Resume Exit_Error
      This is the tedious task that I want to solve.
      What I want is a standard module and then just call the module
      from the Form-Error event. This way, I would not have to copy paste
      and call the standard module from every sub.

      Again, I have accomplished this already, the only problem is that I cannot
      customer the MsgBox per DataErr no bec it only reads the Case Else statement and not the specific Error.

      Thanks for trying.







      Originally posted by puppydogbuddy
      see these links for sample global error handler vs procedural error handler:



      How to trap and log the errors reported while a Microsoft Access database is in use. (Access 95 and onwards.)


      If you agree that procedural error handling is better,safer and more secure.....but wish there is a way to reduce the work effort involved, see this link for a third party tool costing less than $30.

      http://www.aadconsulting.com/errhdoff.html

      Comment

      • Stewart Ross
        Recognized Expert Moderator Specialist
        • Feb 2008
        • 2545

        #4
        Hi Hyperpau. On the value of the dataerr param not being recognised, you are not passing it to your custom error handler from the form_error sub; is it in scope at the time (i.e. global to your current module)?

        The form error event does not trap runtime errors in VBA code (as help entry for this event indicates), so you cannot use it as a general error handler without including an error handler redirect in each procedure, or at least within the module concerned in an appropriate place.

        I tested this by using a command button on a form which on click does a deliberate division by 0. The form_error event was not triggered.

        When I then added a standard On Error Goto to the procedure concerned and redirected from there to the form_error procedure the division by zero was trapped accordingly.I had no problem passing the err.number to my test form_error procedure in place of the dataerr value, nor in displaying the err.description - but as mentioned the trigger was a redirect to the form_error procedure from a normal error redirect in the code concerned.

        Extract from help file on form error event is listed below.

        -Stewart

        Originally posted by MS help file extract for Form Error event
        The Error event occurs when a run-time error is produced in Microsoft Access when a form or report has the focus. This includes Microsoft Jet database engine errors, but not run-time errors in Visual Basic.
        Last edited by Stewart Ross; Aug 5 '08, 09:46 AM. Reason: added note on dataerr param

        Comment

        • puppydogbuddy
          Recognized Expert Top Contributor
          • May 2007
          • 1923

          #5
          To quote Allen Browne from the second link I gave you:

          "Every function or sub should contain error handling. Without it, a user may be left viewing the faulty code in a full version of Access, while a run-time version just crashes."

          If you look at the third party tool, it will relieve you from the tediousness of having to manually add error handling to every procedure.

          Comment

          • hyperpau
            Recognized Expert New Member
            • Jun 2007
            • 184

            #6
            Hi to both of you.

            I completely understand what you are tyring to tell me guys.

            So it simly means that the answer to my question is 'NO'. ??

            Meaning, we cannot use a standard module with a select case statement for each DataErr number and just call it from the Form_Error event of each form?

            If it is really yes, then I guess, I would just have to copy paste the select case statements to the Form_Error event of every form, because I do not want to do the On Error GoTo for each sub.

            But I feel that there is a way to do this because it already works, I am able to call my Select Case statement procedures from my Standard Module, from the Form_Error event of my Form. The only problem i am encountering now is that it does not pass the DataErr number to my standard module, thus only calling the msgbox in the Case Else statement from my Standard Module.

            I wonder what Mary, or Fishval, or missingling would say about this. :)

            Comment

            • FishVal
              Recognized Expert Specialist
              • Jun 2007
              • 2656

              #7
              Hello, hyperpau.

              First and for most lets make an evidence that we are talking about the same. ;)
              As far as I've got it you are looking for a method of handling Form_Error event with a common code in a public code module. The issue is not about handling VBA errors in form module. Am I right?

              If so, then the most convinient way I see so far is to write a public routine which receives:
              • by value error code (Dataerr argument of Form_Error handler)
              • by reference response (Response argument of Form_Error handler) and form object


              Something like the following:

              Form module:
              [code=vb]
              Private Sub Form_Error(Data err As Integer, Response As Integer)
              'form specific code to be run before common code runs
              FormErrorHandle r Dataerr, Response, Me
              'form specific code to be run after common code runs
              End Sub
              [/code]

              Code module:
              [code=vb]
              Public Sub FormErrorHandle r(ByVal Dataerr As Integer, _
              ByRef Response As Integer, _
              ByRef frm As Access.Form)

              Select Case Dataerr
              Case FormErrorCode1
              With frm
              'do some form actions
              End With
              Case FormErrorCode2
              '.....
              End Select

              Response = acDataErrContin ue 'disable standard Access response

              End Sub
              [/code]

              Did you mean something like this? ;)

              Best regards,
              Valentine

              Comment

              • hyperpau
                Recognized Expert New Member
                • Jun 2007
                • 184

                #8
                OH MY GOD!!!!

                It works like a beauty!!!! I knew it!!!!
                I knew there is a way! LOL
                FishVal, you are the greatest!!!!
                I have been looking for this everywhere and here it is, thanks to you!

                Now I don't have to copy paste the Select Case statement to each
                Form's Error event. I just call the Public Sub from my Public Module and all my errors are handled. What the!!!! hahha. Thank you FishVal!!
                This will make my form modules organized and neat for all my next projects.
                You're so great!

                Comment

                • FishVal
                  Recognized Expert Specialist
                  • Jun 2007
                  • 2656

                  #9
                  :)

                  You are welcome.
                  Good luck and happy coding.

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32665

                    #10
                    Originally posted by hyperpau
                    ...
                    I wonder what Mary, or Fishval, or missingling would say about this. :)
                    Nice work Fish!

                    But WHY WASN'T I IN THAT LIST??!? (sulks :D)

                    Comment

                    • FishVal
                      Recognized Expert Specialist
                      • Jun 2007
                      • 2656

                      #11
                      Originally posted by NeoPa
                      Nice work Fish!

                      But WHY WASN'T I IN THAT LIST??!? (sulks :D)
                      :D

                      Are you going to nuke the matrix again, Neo?

                      Kind regards,
                      Fish

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32665

                        #12
                        Some of those powers WOULD be nice :D

                        ...But I'm only NeoPa don't forget.

                        Comment

                        • hyperpau
                          Recognized Expert New Member
                          • Jun 2007
                          • 184

                          #13
                          Originally posted by NeoPa
                          Nice work Fish!

                          But WHY WASN'T I IN THAT LIST??!? (sulks :D)
                          I'm sorry NeoPa, I didn't forget you. I promise!
                          I was previewing the post and found out I clicked the Submit buttone and didn't know how I can UNsubmit it and edit.

                          Hope you're not mad. :(

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32665

                            #14
                            Originally posted by hyperpau
                            ...
                            Hope you're not mad. :(
                            Mad? No! I've only ever been slightly insane :D

                            I hope that wasn't too believable - that I'm quite so egocentric.

                            Comment

                            • hyperpau
                              Recognized Expert New Member
                              • Jun 2007
                              • 184

                              #15
                              For those who may be looking for solution that I have found,

                              I want to share this article. It may not be very technical or I may have not described it in the right naming convention or whatsoever, but it sure works for me. :)

                              Custom Error Messages in Access

                              Comment

                              Working...