ms-access vba Eval函数行为

时间:2021-09-19 11:47:27

I have a public function in an Access form

我在Access表单中有一个公共函数

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?

它显示一个消息框2次。有谁知道它为什么这样做?

I Have Access 2003 with SP3.

我有SP3的Access 2003。

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.

编辑:主要的想法是从该表单调用自定义命令栏控件OnAction的函数。也许你有更好的方法从命令栏控件的表单调用函数。

2 个解决方案

#1


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

这是一个非常长期存在的错误,自访问97天以来一直存在(大约4-5个版本的访问)。

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

这里的解决方案是永远不要使用表单限定符,只需将以下内容放在你的on action事件中,你就可以了。

=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.

更好的是,如果其中一个表单在表单代码模块中没有公共功能,则使用标准代码模块中的函数。所以你可能有九种形式,它们都使用主标准代码模块中的标准一种功能。但是,第10种形式可能需要运行特殊代码,因此您只需将该功能代码放在表单的代码模块中作为公共代码,它将代替标准代码模块中的public运行。

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.

请记住,功能和语法以及我上面提到的所有内容也适用于在功能区中使用on动作进行访问2007时。

#2


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函数而不是eval来调用您的函数。它应该只触发你的函数一次,它仍然允许你参数化表单名称和函数或子名称:

CallByName Forms("MyForm"), "PopupProcess", VbMethod

#1


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

这是一个非常长期存在的错误,自访问97天以来一直存在(大约4-5个版本的访问)。

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

这里的解决方案是永远不要使用表单限定符,只需将以下内容放在你的on action事件中,你就可以了。

=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.

更好的是,如果其中一个表单在表单代码模块中没有公共功能,则使用标准代码模块中的函数。所以你可能有九种形式,它们都使用主标准代码模块中的标准一种功能。但是,第10种形式可能需要运行特殊代码,因此您只需将该功能代码放在表单的代码模块中作为公共代码,它将代替标准代码模块中的public运行。

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.

请记住,功能和语法以及我上面提到的所有内容也适用于在功能区中使用on动作进行访问2007时。

#2


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函数而不是eval来调用您的函数。它应该只触发你的函数一次,它仍然允许你参数化表单名称和函数或子名称:

CallByName Forms("MyForm"), "PopupProcess", VbMethod