By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,161 Members | 1,061 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,161 IT Pros & Developers. It's quick & easy.

Currently running function?

Expert 100+
P: 218
Hi,

Here's a weird one!

Is there a known way to determine the name of the currently running Sub or Function in VBA code?

I'm trying to build a generic error-trapping funtion, to avoid writing hundreds of
Expand|Select|Wrap|Line Numbers
  1. ON ERROR GOTO..
blocks, with different parameter values.

I've been looking at ways to use the Windows API with AddressOf, etc, but I can't see how I can get this to work...

Any hints and suggestions welcomed

TIA

Steve
Dec 8 '06 #1
Share this Question
Share on Google+
11 Replies


NeoPa
Expert Mod 15k+
P: 31,660
As the On Error statement can only branch within the current procedure surely you can just say
Expand|Select|Wrap|Line Numbers
  1. On Error GoTo ErrorCode
  2. ...
  3. ErrorCode:
in each one?
To answer your question though, i don't believe you can access that info from within the code.
Dec 8 '06 #2

Expert 100+
P: 218
As the On Error statement can only branch within the current procedure surely you can just say
Expand|Select|Wrap|Line Numbers
  1. On Error GoTo ErrorCode
  2. ...
  3. ErrorCode:
in each one?
Yeah but... This app I've inherited has some 3500 Subs/Functions !!!
I was trying to avoid that much cutting & pasting (as well as the RSI)

Mebbe I just gotta move away from Access to get some decent exception handling?

I was thinking there may be some hidden calls in the VBA/Access dll's which use this info.

Ah well...

Thanks anyway, NeoPa

Steve
Dec 8 '06 #3

NeoPa
Expert Mod 15k+
P: 31,660
Me.Module.Name will give you the name of the current module at least.
Dec 8 '06 #4

NeoPa
Expert Mod 15k+
P: 31,660
As error handling is handled hierarchically, you can just put it in the higher level functions.
Alternatively, use a bit of code to process through your code in a text file, adding it to all procedures.
I use a text editor that could probably handle that.
Dec 8 '06 #5

ADezii
Expert 5K+
P: 8,669
Hi,

Here's a weird one!

Is there a known way to determine the name of the currently running Sub or Function in VBA code?

I'm trying to build a generic error-trapping funtion, to avoid writing hundreds of
Expand|Select|Wrap|Line Numbers
  1. ON ERROR GOTO..
blocks, with different parameter values.

I've been looking at ways to use the Windows API with AddressOf, etc, but I can't see how I can get this to work...

Any hints and suggestions welcomed

TIA

Steve
'As previously suggested by NeoPa, place the Exception Handler high up in
'the proverbial Call Stack. As for the name of the currently running Sub or
'Function Procedure, a custom Property can be created appropriately named ProcedureName. It can be Set (Property Let) at the beginning of every Procedure and retrieved (Property Get) at will. Hope this helps...
Dec 8 '06 #6

NeoPa
Expert Mod 15k+
P: 31,660
It's just clicked why you want this (no-one ever explains what they really want).
You already know about placing the routine at the top of the call-stack and want a way of setting the error message so that it indicates where the error occurred. Is that right?
Dec 8 '06 #7

Expert 100+
P: 218
It's just clicked why you want this (no-one ever explains what they really want).
You already know about placing the routine at the top of the call-stack and want a way of setting the error message so that it indicates where the error occurred. Is that right?
Yup, right again, NeoPa

Sorry, I clearly didn't get that across in my post. My communication skills tend to disintegrate after 36 hours straight coding!
Dec 8 '06 #8

Expert 100+
P: 218
'As previously suggested by NeoPa, place the Exception Handler high up in
'the proverbial Call Stack. As for the name of the currently running Sub or
'Function Procedure, a custom Property can be created appropriately named ProcedureName. It can be Set (Property Let) at the beginning of every Procedure and retrieved (Property Get) at will. Hope this helps...
Hi ADezii

Thx for your reply

My understanding of custom properties is that they can be applied within class modules. Unfortunately, the offending app is totally procedural (no class modules, no OO!) - hence my difficulties.

I'm not sure whether placing the Exception Handler high up in the Call Stack will work in these circumstances??

TIA

Steve
Dec 8 '06 #9

NeoPa
Expert Mod 15k+
P: 31,660
You could get basic, module level, info as posted earlier, with Me.Module.Name but I know of nowhere that more detailed info is stored or referenced - sorry.
If you want to have a look see at other available properties then select View Watch Window from the view window and add a Watch called Me. You can then check through available items when the code is stopped within that module.
Dec 8 '06 #10

Expert 100+
P: 218
You could get basic, module level, info as posted earlier, with Me.Module.Name but I know of nowhere that more detailed info is stored or referenced - sorry.
If you want to have a look see at other available properties then select View Watch Window from the view window and add a Watch called Me. You can then check through available items when the code is stopped within that module.
Hi NeoPa,
You still around??

Thx for the heads up on this stuff - Me.Module.Name does provide a start, but short of taking a sledgehammer approach, I can't see another way to crack this problem!!

Oh for a version of Try...Catch...Finally ;-)

Thx

Steve
Dec 8 '06 #11

NeoPa
Expert Mod 15k+
P: 31,660
Hi NeoPa,
You still around??

Thx for the heads up on this stuff - Me.Module.Name does provide a start, but short of taking a sledgehammer approach, I can't see another way to crack this problem!!

Oh for a version of Try...Catch...Finally ;-)

Thx

Steve
Steve,

Yeah, I'm still up.
Just thought, if you need the sledgehammer then post #5 may at least be a step in the right direction.

Cheers -Adrian.
Dec 8 '06 #12

Post your reply

Sign in to post your reply or Sign up for a free account.