简体   繁体   中英

ms-access vba Eval function behavior

I have a public function in an Access form

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

When I call

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

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

I Have Access 2003 with SP3.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM