Re: Error trapping code



Thelma, an example might help to explain rkc's suggestion.

Say you have a procedure that calls another proc like this:
Sub MySub
Debug.Print MyFunction()
End Sub
Function MyFunction()
MyFunction = 6
End Function

Now if you add error handling to just MySub, and an error occurs when it
calls MyFunction, the error handler in MySub will catch the problem when it
calls MyFunction and the problem occurs.

While that's true, I prefer to add error handling to MyFunction anyway. The
main reason is that I find it much easier to track down an error if I know
what procedure caused it. Particularly when you get one proc that calls
another that calls another ..., I'm too lazy to try to track down which one
actually caused the error, when I can get Access to gell me just by using
error handling in each one. The other reason is that later on I may need to
call MyFunction() from somewhere else, so I really prefer just to have the
error handlers in place anyway.

As a general rule, it is easier to write software than to maintain it, so
anything you can do to write maintainable code is worth the effort.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"rkc" <rkc@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:N_2Se.49754$EX.9464@xxxxxxxxxxxxxxxxxxxxxxx
> Thelma Lubkin wrote:
>
>> My question is, therefore, should error-trapping code bookend every sub
>> and function? If not, what criteria do I use to decide whether to add
>> it or not?
>
> Functions, methods that return a value to a calling procedure, don't
> necessarily need error handling, but the calling code certainly should
> have it. Objects(class modules) should trap an error and pass it on to the
> client code.


.



Relevant Pages

  • Re: Error trapping code
    ... >: Function MyFunction() ... Now if you add error handling to just MySub, and an error occurs when it ... If you have added error handling to MySub, then when it calls a function ... calling procedure's error handler. ...
    (comp.databases.ms-access)
  • Re: Error trapping code
    ... : Sub MySub ... Now if you add error handling to just MySub, and an error occurs when it ... calls MyFunction, the error handler in MySub will catch the problem when it ... I prefer to add error handling to MyFunction anyway. ...
    (comp.databases.ms-access)
  • RE: Module Question
    ... Private Sub Frame50_AfterUpdate ... My problem is how do I get the SpecialProcess program to go back to point X ... just use the name of the subroutine/function. ... Public Function MyFunction() As Integer ...
    (microsoft.public.access.modulesdaovba)
  • RE: Module Question
    ... regular Access module, just use the name of the subroutine/function. ... Public Sub MySubroutine() ... Public Function MyFunction() As Integer ...
    (microsoft.public.access.modulesdaovba)
  • RE: Calling User-Defined Functions from a macro
    ... The problem is with cell references if ... they are selecting the correct worksheet. ... MyFunction = Target.Value ... Sub Test() ...
    (microsoft.public.excel.programming)

Loading