eval can't find function

J

John B. Smotherman

I have multiple functions that differ in name only by two characters (ie,
DoPage01(), DoPage05(), DoPage10(), etc.) I was calling them using a
Select...Case structure but recent reading pointed me to the Eval function,
and I thought that made much better sense than this really large
Select...Case, so I changed it to an Eval.

I build the function name into a string variable, and use the following:
bDone = Eval(sFunctionName) <==all the functions return a boolean

The problem is the Eval fails with error 2425 - function not found. I've
double-checked the string variable and the function name in it is correct.
Any ideas?


Thanks.
 
T

TedMi

Make sure the functions are declared PUBLIC, and in a standard, not form or
report, module.
-TedMi
 
B

BruceM

You make a good point about making sure the function is available, but AFAIK
there is no reason a function that is used for a single form or report
shouldn't be in that object's module, if that is what is going on here.

To the OP, as a test does it work if you use the actual function name
instead of the variable? What is the code that generates the variable's
value? If you put a break point into the code at the bDone line, do you get
the correct value for the variable. I know you said you double-checked the
variable, but I'm not sure if you mean you tested it at that line of code.
 
C

Clifford Bass

Hi John,

Why not do the Select Case inside of a general DoPage() function instead:

Public Function DoPage(intPage As Integer) As Boolean
{
Select Case intPage
Case 1:
' Do the stuff for page 1

Case 2:
' Do the stuff for page 2

' And so on...

End Select
}

Then you only need to call the function directly with the appropriate
page number (depending on how/where you are specifying the function):

DoPage 1
or
=DoPage(1)

If the processes are lengthy and you really want them in separate
functions you can still use a general function. All you would do in each
Case section would be something like:

DoPage = DoPage01

Clifford Bass
 
J

John Spencer

Well, since you don't show us all your code it is difficult to say.

Assuming that sFunctionName is a string like
== "DoPage05()" Note the inclusion of the parens as the end
== DoPage05 is a function not a sub
== DoPage05 is a PUBLIC function in a VBA module or

If DoPage05 is a PUBLIC function in a form module and the form is open then
the call to eval needs to look more like
Eval("Forms!TheFormName.DoPage05")
And it may run twice.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
D

Dirk Goldgar

BruceM said:
You make a good point about making sure the function is available, but
AFAIK there is no reason a function that is used for a single form or
report shouldn't be in that object's module, if that is what is going on
here.

Except that, in order for the Eval() function to find it, it must be Public
in a standard module.
 
B

BruceM

Thanks for the comment. It seems Eval works differently from other
functions in this regard. For instance, if I place in a form's code module
a function fTest that returns a string, I cannot use that function in Eval,
but I can use it in MsgBox.
Eval("fTest()") fails, but
MsgBox(fTest()) works

However, it works if fTest is in a standard module, as you said. Seems this
would have been documented in the Help information about Eval. Are there
other functions that have this exception?

Most of the examples about Eval in Help don't seem to show what Eval does
that couldn't be done without the extra step, except for being able to use
Between...And or In. Maybe calling a series of functions is a situation
where Eval would be useful, but again I'm not sure I see why the function
needs to be passed through Eval.
 

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