Home  |   French  |   About  |   Search  | mvps.org  

What's New
Table Of Contents
Credits
Netiquette
10 Commandments 
Bugs
Tables
Queries
Forms
Reports
Modules
APIs
Strings
Date/Time
General
Downloads
Resources
Search
Feedback
mvps.org

In Memoriam

Terms of Use


VB Petition

General: Determine name of sub/function where error occurred

Author(s)
Dev Ashish

(Q) I'm having trouble determining at runtime which procedure an error occurred in. Is there a way to get the calling procedure name automatically at runtime?

(A) Unfortunately, VBA doesn't provide you with a property/method to do this automatically. However, you can achieve this through code. Depending on the complexity of what you're trying to achieve, you can either use the 'Pushing' and 'Poping' of procedure names to a global stack as discussed in Access 95 How to by Ken Getz or maintain a global variable that holds the current proc name. For example, create the following variables in a module:

Public pstrProcName as string
Public pstrSubProcName as string

  Now in each of your procedures, at the beginning of the code, assign the current name to these variables. PstrSubProcName should be used when from one function you call another function. For example,

'***** Code Start *******
Sub button1_click()
    PstrProcName = "button1_click()"
    Call sShowMsg
    Msgbox pstrProcName
End sub

Sub sShowMsg()
    
   PstrSubprocName = "sShowMsg"
    
   Msgbox "You are in procedure: " & 
      pstrSubProcName
End sub
'******** Code End *******

  As you can see, if we used pstrProcName to hold the sShowMsg sub name when the control passed to it, upon a successful return to button1_click, pstrProcName would hold an incorrect value of "sShowMsg". Using a pstrSubProcName type variable assures that pstrProcName holds the correct value of current proc.


© 1998-2010, Dev Ashish & Arvin Meyer, All rights reserved. Optimized for Microsoft Internet Explorer