scope inquiry
am 24.12.2007 18:09:41 von mirandacascade
Example 1:
This code behind the form:
Private Function AAA()
MsgBox "hey, I made it!"
End Function
Private Sub Form_Load()
Dim strAAA as String
strAAA = "AAA()"
Eval (strAAA)
End Function
Results in error 2425 "The expression you entered has a function name
that can't find"
Example 2:
When the code behind the form is:
Private Sub Form_Load()
Dim strAAA as String
strAAA = "AAA()"
Eval (strAAA)
End Function
and mod1 is in the project and it has the following code
Function AAA()
MsgBox "hey, I made it!"
End Function
the result is that the "hey, I made it!" messagebox gets displayed,
and no error/exception gets raised.
Questions:
1) in the first case example, is it really the case that the
application is unable to locate the function, or is the 2425 exception
masking some other issue?
2) why is the application able to find AAA() when the code lives in
mod1, but not when the code lives within the same module as the module
in which the function is called?
Thank you.
Re: scope inquiry
am 24.12.2007 18:35:07 von Salad
mirandacascade@yahoo.com wrote:
> Example 1:
> This code behind the form:
>
> Private Function AAA()
> MsgBox "hey, I made it!"
> End Function
>
> Private Sub Form_Load()
> Dim strAAA as String
> strAAA = "AAA()"
> Eval (strAAA)
> End Function
>
> Results in error 2425 "The expression you entered has a function name
> that can't find"
>
> Example 2:
> When the code behind the form is:
>
> Private Sub Form_Load()
> Dim strAAA as String
> strAAA = "AAA()"
> Eval (strAAA)
> End Function
>
> and mod1 is in the project and it has the following code
>
> Function AAA()
> MsgBox "hey, I made it!"
> End Function
>
> the result is that the "hey, I made it!" messagebox gets displayed,
> and no error/exception gets raised.
>
> Questions:
> 1) in the first case example, is it really the case that the
> application is unable to locate the function, or is the 2425 exception
> masking some other issue?
> 2) why is the application able to find AAA() when the code lives in
> mod1, but not when the code lives within the same module as the module
> in which the function is called?
>
> Thank you.
Why don't you read the thread "dynamically calling subroutines" you
started on the 21st?
Black & White
http://www.youtube.com/watch?v=OuwSLjLtJWo
Re: scope inquiry
am 24.12.2007 18:59:00 von mirandacascade
On Dec 24, 9:35=A0am, Salad wrote:
> Why don't you read the thread "dynamically calling subroutines" you
> started on the 21st?
Apologies in advance for sounding dense with this follow up...still
not seeing where in the various replies in the other thread, the issue
in this thread was covered.
1) one reply (the reply from Mr. Gillespie) mentions that when using
Eval(), the string passed to the string passed to the Eval() function
must contain the name of a function...not a subroutine...I believe the
examples in this thread are all where the string passed to the Eval()
function contains the name of a function...not a subroutine
2) other replies in that thread mention the fact that Eval() can
return a value...but I'm not seeing how that could explain the issue
raised in this thread...namely that the function is found when the
code resides on one module, but it is not found when the code resides
in a different module
The answer is probably hiding in plain sight in the previous thread,
but would someone be able to point out which reply in that other
thread answers the question asked in this thread?
Thanks (and apologies again for missing something obvious)
Re: scope inquiry
am 24.12.2007 19:52:13 von lyle
On Dec 24, 12:59 pm, mirandacasc...@yahoo.com wrote:
> On Dec 24, 9:35 am, Salad wrote:
>
> > Why don't you read the thread "dynamically calling subroutines" you
> > started on the 21st?
>
> Apologies in advance for sounding dense with this follow up...still
> not seeing where in the various replies in the other thread, the issue
> in this thread was covered.
>
> 1) one reply (the reply from Mr. Gillespie) mentions that when using
> Eval(), the string passed to the string passed to the Eval() function
> must contain the name of a function...not a subroutine...I believe the
> examples in this thread are all where the string passed to the Eval()
> function contains the name of a function...not a subroutine
>
> 2) other replies in that thread mention the fact that Eval() can
> return a value...but I'm not seeing how that could explain the issue
> raised in this thread...namely that the function is found when the
> code resides on one module, but it is not found when the code resides
> in a different module
>
> The answer is probably hiding in plain sight in the previous thread,
> but would someone be able to point out which reply in that other
> thread answers the question asked in this thread?
>
> Thanks (and apologies again for missing something obvious)
I recall a thread from many years ago that postulated that Expression
in Eval(Expression) is globally scoped and not modularly scoped. If
that's so, then Eval will not be able to find or have access to AAA()
when it AAA() exists as a private function in a Form Class module.
You could test this by making the function Public and using
Eval("Form_NameofForm.AAA()").
I think I've not seen including "()" in Function and Sub Calls with
Eval, and it seems strange to me as the name of the procedure is "AAA"
not "AAA()". But, apparently it has worked for you, so why knock it.
In other languages we say that "()" is the function operator.
Re: scope inquiry
am 24.12.2007 20:02:10 von Salad
mirandacascade@yahoo.com wrote:
> On Dec 24, 9:35 am, Salad wrote:
>
>>Why don't you read the thread "dynamically calling subroutines" you
>>started on the 21st?
>
>
> Apologies in advance for sounding dense with this follow up...still
> not seeing where in the various replies in the other thread, the issue
> in this thread was covered.
>
> 1) one reply (the reply from Mr. Gillespie) mentions that when using
> Eval(), the string passed to the string passed to the Eval() function
> must contain the name of a function...not a subroutine...I believe the
> examples in this thread are all where the string passed to the Eval()
> function contains the name of a function...not a subroutine
>
> 2) other replies in that thread mention the fact that Eval() can
> return a value...but I'm not seeing how that could explain the issue
> raised in this thread...namely that the function is found when the
> code resides on one module, but it is not found when the code resides
> in a different module
>
> The answer is probably hiding in plain sight in the previous thread,
> but would someone be able to point out which reply in that other
> thread answers the question asked in this thread?
>
> Thanks (and apologies again for missing something obvious)
My apologies. I read your post without fully reading it. From your
post, your code works in a module and doesn't work in the form's module.
Maybe Eval expects a public function without a reference to the form module.
If I were presented with something that works and something that
doesn't, I'd go with what works.
Re: scope inquiry
am 24.12.2007 21:52:23 von XXXusenet
mirandacascade@yahoo.com wrote in
news:91a5c599-a33a-4b1d-8579-e7cb09ea0a71@e25g2000prg.google groups.co
m:
> Questions:
> 1) in the first case example, is it really the case that the
> application is unable to locate the function, or is the 2425
> exception masking some other issue?
> 2) why is the application able to find AAA() when the code lives
> in mod1, but not when the code lives within the same module as the
> module in which the function is called?
I'm actually surprised at what you found, but it seems clear that
Eval() is *not* running in the context you think it is. That is, it
doesn't pick up the local scope of the form. When you put it in the
standalone module without a Private or Public, I would guest it is,
by default, Public, so it's therefore available to Eval().
Have you tried Lyle's suggestion, of fully qualifying the function
call with the form name?
I don't use Eval() enough to have experience with this. Is there an
important reason why you need to use it?
--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Re: scope inquiry
am 27.12.2007 17:18:52 von Marshall Barton
mirandacascade@yahoo.com wrote:
>On Dec 24, 9:35 am, Salad wrote:
>> Why don't you read the thread "dynamically calling subroutines" you
>> started on the 21st?
>
>Apologies in advance for sounding dense with this follow up...still
>not seeing where in the various replies in the other thread, the issue
>in this thread was covered.
>
>1) one reply (the reply from Mr. Gillespie) mentions that when using
>Eval(), the string passed to the string passed to the Eval() function
>must contain the name of a function...not a subroutine...I believe the
>examples in this thread are all where the string passed to the Eval()
>function contains the name of a function...not a subroutine
>
>2) other replies in that thread mention the fact that Eval() can
>return a value...but I'm not seeing how that could explain the issue
>raised in this thread...namely that the function is found when the
>code resides on one module, but it is not found when the code resides
>in a different module
>
>The answer is probably hiding in plain sight in the previous thread,
>but would someone be able to point out which reply in that other
>thread answers the question asked in this thread?
>
>Thanks (and apologies again for missing something obvious)
I don't think it's at all obvious, but you maybe you can
help yourself keep at least part of it straight in your
mind. Always declare module level variables, functions and
subs either Private or Public instead of the defaulting to
Private in class modules and Public in standard modules.
IMO, Lyle has it right and you should analyze/experiment as
he suggested.
Here's a couple of other experiments using the Immediate
window that demonstrate Eval using the Expression sevice
outside of the VBA context.
?2 IN (1,2,3) -> Expected: expression error
?Eval("2 IN (1,2,3)")
-1
?acForm
2
?Eval("acForm") ->can't find the name 'acForm' error
--
Marsh