MSAccess 2003 - 用于将值从一种形式传递到另一种形式的 VBA [英] MSAccess 2003 - VBA for passing a value from one form to another

查看:19
本文介绍了MSAccess 2003 - 用于将值从一种形式传递到另一种形式的 VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

谢谢.

解决方案

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

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

方法如下:

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

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

选项比较数据库选项显式将 frmPrevious 作为表单

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

设置 frmPrevious = Screen.ActiveForm

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

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

frmPrevious.Refresh

如果要设置ID值,那么去:

frmPrevious!ID = 某个值

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

frmPrevious.frmPrevious!ID = 某个值

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

frmPrevious.MyCustomRefresh

甚至诸如某些强制以前的表单生成和设置发票编号之类的选项:

frmPrevous.SetInvoice

frmPrevious.SetProjectStatusOn

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

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

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

这种方法意味着以前的大部分表单现在都触手可及.

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

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.

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.

Thanks.

解决方案

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.

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.

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.

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:

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:

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:

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.

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.

这篇关于MSAccess 2003 - 用于将值从一种形式传递到另一种形式的 VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆