views:

905

answers:

2

I have a public function in access form

Public Function PopupProcess() as long
 MsgBox Me.ActiveControl
 PopupProcess = 1
End Function

when i call

eval("forms('MyForm').popupprocess")

it shows message box 2 times. Does anybody know why it does that?

I Have Access 2003 with SP3.

thanks.

EDIT : Main idea is to call function from that form for Custom Commandbar control OnAction. Maybe you have better way to call function from a form for commandbar control.

+1  A: 

No idea. What happens if you call it like this?

Call Forms("MyForm").PopupProcess

Try using the CallByName function, instead of eval, to call your function. It should only fire your function once, and it will still allow you to parameterize the form name and the function or sub name:

CallByName Forms("MyForm"), "PopupProcess", VbMethod
Robert Harvey
It shows MessageBox only 1 time.
THEn
THEn, see my edit.
Robert Harvey
The above can’t work in this case. The poster is talking about custom menu bars, not a button on a form. In custom menu bars you only have the onAction setting, and for it to call your code you must use an expression, not “Call subname”.
Albert D. Kallal
You could call a public function that calls the function on form.
David-W-Fenton
+5  A: 

This is a very long standing bug that’s been around since the days of access 97, (about 4-5 versions of access).

The solution here is to NEVER use the forms qualifier, simply place the following in your on action event, and you’ll be just fine

=PopUpProcess()

Note that you must precede it with=, and the suffix must have the brackets ()

Keep in mind that you can actually use behavior to your advantage. The function that runs is going to be from the form that currently has the focus on the screen. That means you can have different forms with the same name of the function, and whichever form has the focus, that function with that name will run from that forms code module.

Even better, if one of the forms does not have that function as public in the forms code module, then the function in a standard code module is used. So you might have nine forms, that all use the standard one function in the main standard code module. However, the 10th form might need to run special code, so you simply place that function code in the form’s code module as public and it will run in place of the public on in the standard code module.

This approach allows you to build a single custom menu bar that applies to many different forms, but those many forms will run different code on from that custom menu bar. This also encourages you to place the menu code in the form it belongs.

So to solve your problem, simply don’t use a form’s qualifier, and use the above format.

Note that you can pass Parameters from those functions also, for example

=PopUpProcess(‘hello’)

And then declare the function as:

Public Function PopUpProcess(strParm as string)

Keep in mind that the function and syntax and all of what I stated above also applies to when you use the on action in a ribbon for access 2007.

Albert D. Kallal
+1. nice answer
Mitch Wheat
Very nice indeed ! But I have the impression that Access 2007 has a changed behaviour, and that the public functions of a form are no more usable outside of VBA (e.g. as the source of a calculated control). Can someone confirm ?
iDevlop
I am not aware of any change in this behavior with regards to 2007. For a public function in a form called mytest, then =mytest() can be the source of a control on that form. You still must use the public keyword, and we are taking about a form that has the current focus. As far as I can tell, I don't see any different behaviors in how this works in 2003,or 2007.
Albert D. Kallal