Eval Function - Recursive call?? Routine Being Called Multiple Times

Joined
Dec 30, 2007
Messages
1
Reaction score
0
Hi there

I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with or without parms. I know that any function that is passed to Eval() must be declared Public. It can be a Sub or Function, as long as it's Public. I even have it where the "function" evaluated by Eval can be in a form (class) module or in a standard code module (and if in a form module, as long as the call is fully qualified, I can call it). That's all good.That's not the problem. The problem is with the # of times my routine that houses my Eval function call is run by Access.

I have done some research on this, but it appears that those out there having the same problem have never really gotten a good answer to/reason for this problem (just some work arounds I imagine)

Research Links:

Here problem did get a resolution, but Dirk's post shows that he was having problems with the function being called twice too:
http://awomantoldme.com/women/how-to/31178849/why-does-this-code-not-wo.aspx

Same posting: http://www.microsoft.com/communitie...&mid=745cfede-8c04-44ec-969a-6d325a8633e8&p=1

And there was another older posting I've seemed to have lost the link to where the poster was having the same recursive call problem, but there were no responses ever posted ;-(

SCENARIO
=========

I have a Main Form (frmMain) containing 2 subforms (fsubChild1 and fsubChild2).

The SourceObject value for fsubChild2 is constantly being changed to show whatever form I am working on at the time (think of frmMain as simply a container form for my 2 subforms and really does nothing more and has no other controls on it, other than an Exit Application button)
fsubChild1 contains labels, that, when clicked on, excutes a standard function that must exist in the code-behind-form for any valid form that can be plugged in as the SourceObject for fsubChild2. Let's call this standard function mtdRunTheCode. It exists as a PUBLIC function in every form that could potentially be the SourceObject for fsubChild2 and is passed the name of a function to execute, depending on the label clicked in fsubChild1 (each label in fsubChild1 passes mtdRunTheCode a different function. Think of mtdRunTheCode() as a wrapper function. It always contains the Eval(strFunction) code line, but may also contain code or calls to other routines specific to the form in which it is executing. I wanted to do this so that fsubChild1 could call generic functions (located in a standard code module) that must be run when a label is clicked (no matter what form is contained in fsubChild2), but, depending on the form in fsubChild2, can run code specific to fsubChild2 first (such as validating the current record, saving, etc etc)

Let's imagine that I had 2 forms: frmCust and frmProd.

Both of these forms at any given time, could be the SourceObject for fsubChild2

Both of these forms contain their own version of mtdRunTheCode - declared Public, named the same and located in the subform's form module (ie the function becomes a "method" of the form)..

This is how the function looks in frmCust
Code:
Public Function mtdRunTheCode(strFunction)
 
 
 


[indent][color=green]'This function is called by the OnClick event of a given label in fsubChild1[/color]

 
 
 
[color=green]'The value of strFunction changes, depending on the label that was clicked[/color]
[color=green]'and contains the name of a unique, public function to execute. All of these[/color]
[color=green]' public functions are contained in a single Code Module.[/color]
[color=green]'Code that is specific to this form[/color]
 
Application.RunCommand acCmdSaveRecord
 
[color=green]'Execute the general function passed in by clicking a label in fsubChild1[/color]
 
Eval(strFunction)
 
 
 

[/indent]End Function


And this is how that same function it looks in frmProd
Code:
Public Function mtdRunTheCode(strFunction)
 
 
 


[indent][color=green]'This function is called by the OnClick event of a given label in fsubChild1[/color]

 
 
 
[color=green]'The value of strFunction changes, depending on the label that was clicked[/color]
[color=green]'and contains the name of a unique, public function to execute. All of these[/color]
[color=green]' public functions are contained in a single Code Module.[/color]
[color=green]'Code that is specific to this form[/color]
 
DoCmd.OpenReport "rptSomeName"
etc
 
[color=green]'Execute the function passed in by clicking a label in fsubChild1[/color]
 
Eval(strFunction)
 

[/indent]End Function

The Calling Code in fsubChild1
Code:
Private Sub lblTask1_Click()[indent]Call Forms!frmMain!fsubChild2.mtdRunTheCode("udfGeneralFunc()")

 

[/indent]End Sub

 
 
 
 
 
 


Private Sub lblTask2_Click()[indent]Call Forms!frmMain!fsubChild2.mtdRunTheCode("udfAnotherFunc()") 

 

[/indent]End Sub

And finally, the generic function located in it's own standard code module
Code:
Public Function udfGeneralFunc() As Boolean[indent]MsgBox "This is nifty stuff"

 

[/indent]End Function

By doing the above, I can utilize the same general functions for an OnClick of a lable, no matter what subform is in fsubChild2, yet execute form specific code first, if needed.

THE PROBLEM
============

I don't get errors if I try to run Eval routine in any of the the mtdRunTheCode() routines. What happens is that the mtdRunTheCode routine runs TWICE. I tracked it and when I click the label in fsubChild1, the following events occur:

- Calls mtdRunTheCode
- Executes the Eval function (which in turns calls the passed in function located in a code module)
- Recursively calls mtdRunTheCode again
- Re-executes the Eval function again.

So for example, if the strFunction arg passed to mtdRunTheCode was a call to the public function udfOpenTheForm('frmTest'), and udfOpenTheForm simply ran the openform command to open the frmTest form (in Normal Window mode), this is what happens

- Calls mtdRunTheCode
- Executes the Eval function (calls udfOpenTheForm which opens the frmTest form)
- Recursively calls mtdRunTheCodeagain
- Re-executes the Eval function again (recalls udfOpenTheForm)

If udfOpenTheForm opens the form in dialog mode, since that halts execution until the form is closed, it causes different problems, making the user click twice to close that form ->

- Calls mtdRunTheCode
- Executes the Eval function (calls udfOpenTheForm which opens the frmTest form in acDialog mode). Waits until user closes form.
- Recursively calls mtdRunTheCode again
- Re-executes the Eval function again (recalls udfOpenTheForm)
- waits until user closes the form, then finally leaves the mtdRunTheCode routine

I don't really want to put the mtdRunTheCode() in a standard code module, because I really want to be able to execute form specific things before executing the code resulting from a label's onclick event (and I may not want to do that for a different form)

Does anyone know the reason for the recursive call?? I've been struggling with this for a bit now and it's driving me nuts.

Thanks!
RandomElle
 

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