Need to obtain query name from within called VBA function - HOW?

M

mikes

I have written a function that needs to know the query name that the
function is being called from. I would prefer NOT to pass a string. The
"desired" approach would be to use something like Me.querydef.name, but
the ME keyword is not supported in this context. I would also prefer
not to have to hard code the query name as a string in the argument if
possible.

Any thoughts?

TIA,
Mike
 
D

Dirk Goldgar

I have written a function that needs to know the query name that the
function is being called from. I would prefer NOT to pass a string.
The "desired" approach would be to use something like
Me.querydef.name, but the ME keyword is not supported in this
context. I would also prefer not to have to hard code the query name
as a string in the argument if possible.

Any thoughts?

I think you can't do it. <wry grin> It *may* be that there's some way
to reach down into the internals of the Jet database engine and find out
something about the query currently being evaluated, or reach into the
Expression Service to find out where the expression currently being
evaluated came from, but if so, it isn't public information. Bear in
mind also that a function could be called from a query that exists only
as an SQL string being executed, not stored in a QueryDef at all.

You could, of course, pass a numeric code of some sort to the function,
instead of the query name. That would at least free you up from
concerns about the query maybe being renamed. The most likely reason
for the function needing to know what query is calling it is so that the
behavior of the function can vary accordingly. In that case, what
you're really passing is an instruction to the function to behave in a
particular way, and that could be logically independent of the actual
name of the query.

Another possibility is to isolate the different behaviors of the
function into separate subfunctions, with distinct "superfunctions" that
string together calls to these subfunctions to support different sets of
behaviors. So different queries would call different "superfunctions",
depending on their needs.

If you really need to know the name of the query being executed, in some
very simple environments you could set a global variable with the name
of the query that is about to be executed, and then execute the query th
at calls the function. The function would retrieve the name of the
query from the global variable. But this won't work if you have more
than one of these queries running at the same time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top