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

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

问题描述

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

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

谢谢.

解决方案

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

请记住,ms-access中的每种形式实际上都是可以在代码中操作的类对象.因此,在这里使用一种对象方法,您会发现不仅编写了更少的代码,而且您的代码将更加简洁,模块化,并且不需要全局变量,并且您编写的代码经常可以在不同的形式之间重复使用. >

方法如下:

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

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

Option Compare Database
Option Explicit 
dim frmPrevious       as form 

然后,在表单加载事件中,我们进行以下操作:

Set frmPrevious = Screen.ActiveForm

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

因此,如果要强制以先前形式进行磁盘写入,然后重新加载数据.

frmPrevious.Refresh

如果要设置ID值,请执行:

frmPrevious!ID = some value

并且,请注意,您甚至可以将先前的表单声明为该表单的PUBLIC变量,因此,如果您将两个表单深化,则可以执行以下操作:

frmPrevious.frmPrevious!ID = some value

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

frmPrevious.MyCustomRefresh

甚至是某些诸如强制前一张表格生成并设置发票编号的选项之类的东西:

frmPrevous.SetInvoice

frmPrevious.SetProjectStatusOn

因此,您不仅可以来回移动值和数据,还可以轻松地执行为先前形式在代码中构建的功能.

事实上,作为一种编码标准,我的MOST表单具有一个称为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天全站免登陆