MSAccess 2003 - 用于将值从一种形式传递到另一种形式的 VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1462876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:01:27  来源:igfitidea点击:

MSAccess 2003 - VBA for passing a value from one form to another

ms-accessvbaparametersparameter-passing

提问by Justin

So how can I pass a value from one form to another? For example: The user select's an organization from a list and this opens up a trip form that allows a user to enter various information regarding the trip. At one place I would like to add another little pop up form where they can enter contact information (just a name and phone for POC) of the organization they are visiting.

那么如何将值从一种形式传递到另一种形式呢?例如: 用户从列表中选择一个组织,这会打开一个旅行表单,允许用户输入有关旅行的各种信息。在一个地方,我想添加另一个小的弹出表单,他们可以在其中输入他们正在访问的组织的联系信息(只是 POC 的姓名和电话)。

So when that initial form opened from the selection screen it has two IDs that are simply hidden in text boxes (one being the tripID, and the other being the OrgID), so how do I pass these to the second little pop up form so that the contact information has the relative IDs with it.

因此,当从选择屏幕打开初始表单时,它有两个 ID,它们只是隐藏在文本框中(一个是 tripID,另一个是 OrgID),那么我如何将它们传递给第二个小弹出表单,以便联系信息具有相关的 ID。

Thanks.

谢谢。

回答by Albert D. Kallal

The best approach in these cases is not to attempted to pass a bunch of variables. It is too much code, and is inflexible. For example, if you need to pass two values, what happens over the years when that requirement grows to 5 values? Trying to maintain and pass a whole whack of values is too much coding work.

在这些情况下,最好的方法是不要尝试传递一堆变量。代码太多,不灵活。例如,如果您需要传递两个值,那么当该需求增长到 5 个值时会发生什么?试图维护和传递一整套值是太多的编码工作。

Keep in mind that each form in ms-access is really a class object that you can manipulate in code. So, use a object approach here and you find you not only write less code, but your code will be more clean, more modular, no need for global vars, and code you write can often be re-used between different forms.

请记住,ms-access 中的每个表单实际上都是一个可以在代码中操作的类对象。所以,在这里使用对象方法,你会发现你不仅编写更少的代码,而且你的代码会更干净,更模块化,不需要全局变量,并且你编写的代码通常可以在不同的形式之间重用。

Here is how:

方法如下:

In general when one form launches another form in the 2nd form in the forms on-open event (in fact, you can even use as late as the on-load event) you can pick up a reference to the PREVIOUS form object. In other words, you can use a object approach here.

通常,当一个表单在表单打开事件中的第二个表单中启动另一个表单时(实际上,您甚至可以在加载事件之后使用),您可以获取对 PREVIOUS 表单对象的引用。换句话说,您可以在这里使用对象方法。

At the forms module level, for form I declare a form object as:

在表单模块级别,对于表单,我将表单对象声明为:

Option Compare Database
Option Explicit 
dim frmPrevious       as form 

Then, in the forms on-load event, we go:

然后,在表单加载事件中,我们去:

Set frmPrevious = Screen.ActiveForm

Now, any code in our form can FREELY use code, events, even varibles declared as public from that previous form in code.

现在,我们表单中的任何代码都可以自由使用代码、事件,甚至是代码中先前表单中声明为公共的变量。

So, if you want to force a disk write of the previous form, and re-load of data.

所以,如果你想强制磁盘写入以前的形式,并重新加载数据。

frmPrevious.Refresh

If you want to set the ID value, then go:

如果要设置 ID 值,则转到:

frmPrevious!ID = some value

And, note that you can even declare form previous as a PUBLIC variable for that form, and thus if you two forms deep, you could go:

而且,请注意,您甚至可以将 form previous 声明为该表单的 PUBLIC 变量,因此如果您有两个表单深,您可以:

frmPrevious.frmPrevious!ID = some value

So, simply declare a forms object in EACH forms code module (or at lest the ones where you need to use values in code). The above means any code has a ready made reference to the previous form object. Functions declared as public in a form will become a METHOD of the form, and can be run like:

因此,只需在每个表单代码模块中声明一个表单对象(或者至少是您需要在代码中使用值的那些)。以上意味着任何代码都有一个对前一个表单对象的现成引用。在表单中声明为 public 的函数将成为该表单的 METHOD,并且可以像这样运行:

frmPrevious.MyCustomRefresh

or even things like some option to force the previous form to generate and setup a invoice number:

甚至像某些选项可以强制以前的表单生成和设置发票编号:

frmPrevous.SetInvoice

or

或者

frmPrevious.SetProjectStatusOn

So not only can you shuffle values and data back and forth, but you can easily execute features and functions that you build in code for the prevous form.

因此,您不仅可以来回调整值和数据,还可以轻松地执行您在代码中为前一个表单构建的特性和函数。

In fact as a coding standard, MOST of my forms have a public function called MyRefresh.

事实上,作为一种编码标准,我的大多数表单都有一个名为MyRefresh.

Note that the beauty of this approach is that you can thus read + use + set values from that previous form. This allows your code to not only receive values, but also set values in that previous form. So this approach is bi-directional. You can shuffle data and values back and forth between the forms. The other advantage here is you NOT restricted to just variables, but can use fields, control values (events, properties) etc.

请注意,这种方法的美妙之处在于您可以从以前的表单中读取 + 使用 + 设置值。这使您的代码不仅可以接收值,还可以以先前的形式设置值。所以这种方法是双向的。您可以在表单之间来回移动数据和值。这里的另一个优点是您不仅限于变量,还可以使用字段、控制值(事件、属性)等。

This approach means that much of the previous form is now at your fingertips.

这种方法意味着以前的大部分形式现在都触手可及。

So don't try to pass a whole whack of variables. Pass a reference to the form and you have a nice ready made object at your fingertips and it makes this type of coding problem a breeze.

所以不要试图传递一大堆变量。传递对表单的引用,您就拥有一个触手可及的漂亮现成对象,它使此类编码问题变得轻而易举。

回答by Robert Harvey

The usual way would be to reference the textboxes in the initial form from the popup form, like this:

通常的方法是从弹出表单中引用初始表单中的文本框,如下所示:

Forms!frmInitialForm!tripID
Forms!frmInitialForm!OrgID

However, this tightly binds the popup form to the initial form, so that it cannot be used anywhere else in the application.

然而,这将弹出窗体与初始窗体紧密绑定,因此它不能在应用程序的任何其他地方使用。

A better approach is to use OpenArgs:

更好的方法是使用 OpenArgs:

DoCmd.OpenForm "frmPopup", OpenArgs:=Me.tripID & ", " & me.OrgID

This places your two values into a string, which is passed to the popup form. You can then parse the two values out of the OpenArgs using the Split function.

这会将您的两个值放入一个字符串中,该字符串将传递给弹出窗体。然后,您可以使用 Split 函数从 OpenArgs 中解析这两个值。

For more info about passing parameters using OpenArgs, see: http://www.fmsinc.com/free/NewTips/Access/accesstip13.asp

有关使用 OpenArgs 传递参数的更多信息,请参阅:http://www.fmsinc.com/free/NewTips/Access/accesstip13.asp