(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,
Sub button1_click()
PstrProcName = "button1_click()"
Call sShowMsg
Msgbox pstrProcName
End sub
Sub sShowMsg()
PstrSubprocName = "sShowMsg"
Msgbox "You are in procedure: " &
pstrSubProcName
End sub
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. |