Subrouting name into variable

  • Thread starter Thread starter MeSteve
  • Start date Start date
I am trying to set up an error log and want to trap the subroutine name where
the error happened. I can hard code into each sub, but would like to break
it into a separate module and call from different subs.
 
No: unfortunately, VBA does not expose the procedure stack to us.
It obviously knows what procedure is running, and where it was called from,
and so forth back to the top, but there is no way AFAIK to get at that
information.

What I do is to use the VBA utility from www.mztools.com to drop the error
handler into each procedure. It gives you an extra toolbar in your VBA
window, and one of the toolbar buttons drops the procedure name into the
error handler, which I then pass to a generic error handler.

Users don't write down the error messages for you, so it's worth logging
them to a table where you can see them again later. Here's how:
http://allenbrowne.com/ser-23a.html

Mztools lets you define how you want your error handler, so FWIW, this is
what I use:

'On Error GoTo Err_Handler
'Purpose:
{PROCEDURE_BODY}

Exit_Handler:
Exit {PROCEDURE_TYPE}

Err_Handler:
Call LogError(Err.Number, Err.Description, conMod & ".{PROCEDURE_NAME}")
Resume Exit_Handler

Notes:
====
1. You will notice how it comments out the error handler, until the
debugging is done. A global search'n'replace activates them all later.

2. The "conMod" is a private constant at the top of each module, containing
the name of the module, e.g.:
Private Const conMod = "Form_Form1"
Every module gets this constant. Now if you move/duplicate code to different
modules, it still reports the correct module to the error logger.

HTH.
 
Good, I was on the right track. What I have so far isn't quite as functional
but was working. I will definately incorporate some of the things mentioned.
Thanks again.
 
Back
Top