Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you call a procedure in a form's class module from a function?

Status
Not open for further replies.

Schof

Programmer
Nov 18, 2002
103
CA
I have the need to call a form's (public) procedure from a function where the procedure name is passed as a parameter. I have managed to get this working using the EVAL function but the problem is that it runs twice. Does anyone know of how else this can be accomplished (without moving all of my procedures to standard modules)?

For example, in my application I have created popup menus and have set the action as "=processShortcutMenu()". I have defined that function in a standard module as:

Public Function processShortcutMenu()
Dim cmdbarCtl As CommandBarControl

Set cmdbarCtl = CommandBars.ActionControl
Eval ("Forms(" & Chr$(34) & application.CurrentObjectName & Chr$(34) & ")." & cmdbarCtl.Tag)
End Function


The commandbar tag is the name of the procedure in the form that I want to run. This works but it runs mulitple times.

I am also trying to use this concept in other areas other than popup menus so it is very important for me to figure out how to do this.
 
It might be beter to put this in a global module, even if you had to put multiple functions in a global module; it would work more efficiently there.

David
 
Putting over 200 functions in a global module is not an option for me. Especially since the example I gave is only one instance of how I want to use this.

I have checked every news group / forum I can find and the solution to this problem is not in any of them. However, there are hundreds of postings of people trying to do this so there must be a way to do it.
 
You can get around the double execution but this is really becoming spaghetti code.

First, you need to understand why the form's routine is being called twice. EVAL constructs a string and then evaluates it (tries to execute that string as code). The first call is being done to construct and the second to run it.

If you insist on using this, and since you have hundreds of these, you need to set up a Public boolean variable. You should probably put it in the module with processShortcutMenu. Add a line to set the boolean to false (indicating the form's routine has not yet executed) before the Eval.

Public mblnEval As Boolean

Public Function processShortcutMenu()
Dim cmdbarCtl As CommandBarControl

Set cmdbarCtl = CommandBars.ActionControl
mblnEval = False
Eval ("Forms(" & _
Chr$(34) & Application.CurrentObjectName & Chr$(34) & ")." & cmdbarCtl.Tag)

End Function

Then in the form's routine add at the beginning:
If mblnEval Then
Exit Sub
Else
mblnEval = True
End If

This will reset your boolean and proceed the way you want.
You'll still be going thru the motions of calling the routine twice, but the actual code will only run once.

I'm sure that this can be done more easily via API calls, but never having had the need to do this, I haven't given it much thought. I'll have to check it out.
 
Are these 200 Functions actually 200 different functions?

Or are the differences related to the different forms they are a part of?

David
 
First off, thx for the replies.

jiqjaq
I thought about doing this but there are a couple of problems with that approach.

1) I would have to add the boolean check to all of my form's routines, which are too numerous to do.

2) The processShortcutMenu() function only executes once, so the boolean is never checked.

dpimental
Unfortunately all of the functions are different, otherwise I would have put them in a standard module. The main reason I don't want to move the procedures to a standard module (other than the amount of time it would take) is that the procedures are only relevant to the form's class module where they are already located. Also, it would make it extremely cumbersome to maintain the code in the future and since the application is already a couple of years old I don't want to destroy it because of this limitation. Worst case scenario I will simply remove the functionality, which I don't want to do!!!
 
I knew there was a way to do it. Enjoy!!!

Public Function processShortcutMenu()
Dim frm As Form
Dim cmdbarCtl As CommandBarControl

Set cmdbarCtl = CommandBars.ActionControl
Set frm = Forms(application.CurrentObjectName)

CallByName frm, cmdbarCtl.Tag, VbMethod
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top