Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-窗体/数据页

【译文】当在VBA代码中引用子窗体时使用窗体属性

时 间:2012-04-08 10:14:07
作 者:Chris Downs   ID:24526  城市:上海
摘 要:窗体变量使得我的代码更快捷且容易阅读,同时也提供了一个当焦点不在其他窗体上时控制其他窗体的好方法。另外一个优点是窗体读取记录值比从表中获得记录集的速度要快。如果你知道你要寻找的数据已在窗体加载,用窗体变量的方式读取数据它能使你的应用程序速度更快。
正 文:

原作者:Chris Downs 翻译:周芳

【译文】窗体变量能使你的代码顺利
        我喜欢使用窗体变量,这使得我的代码更快捷且容易阅读,同时也提供了一个当焦点不在其他窗体上时控制其他窗体的好方法。另外一个优点是窗体读取记录值比从表中获得记录集的速度要快。如果你知道你要寻找的数据已在窗体加载,用窗体变量的方式读取数据它能使你的应用程序速度更快。
        例如,假定我在frmOrders窗体中写了代码,需要去读取一个打开状态的frmCustomers窗体上的控件(你可以传递一个窗体变量到一个打开的窗体)。我使用下面的代码作为变量示例:
        Dim frm as Form_frmCustomers ‘声明frm为Form_frmCustomers
        Dim lngCustomerID as Long     ’声明lngCustomerID为数字型(长整型)
        ‘如果frmCustomers窗体处于打开(加载)状态
        If CurrentProject.AllForms("frmCustomers").IsLoaded then
                Set frm = Forms!frmCustomers
                '在外部窗体用窗体变量读取字段
                lngCustomerID = frm.CustomerID
        Else
                '用其他代码找到customerID
        End If
        有一个优势,声明frm为Form_frmCustomers来代替Form类可以弹出智能提示来帮助您引用窗体上的控件。你可以在任何处于打开状态的窗体上进行这个操作,只需确保您使用声明此类型的变量时,在命名窗体时前面是以"Form_"开头。


子窗体同样奏效,但是需要使用窗体属性
        子窗体作为主窗体的一部分被加载时就会失去自己的窗体特性。当引用一个子窗体时,你需要用主窗体的性能来关联子窗体的窗体变量。让我们假设frmOrders为主窗体,让frmOrderItems_sub以子窗体插入 frmOrders。 如果你需要引用子窗体,你可以在下面的内容中这样写:
        Dim frm as Form_frmOrderItems_sub
        Set frm = Forms!frmCustomers.frmOrderItems_sub.Form
        注意参考窗体属性在结束部分的第二次声明,为了使子窗体和窗体变量关联,这是必要的 

        注1:你只能在窗体模块(或模块)中使用窗体变量,否则,编译器就会报错。
        注2:确保引用正确的子窗体名字否则代码将不能运行。例如,如果子窗体控件的源对象名为"frmOrderItems_sub”,但子窗体控件名称为“OrderItems”在主窗体上,那么你的代码像这样:
        Dim frm as Form_frmOrderItems_sub ‘声明frm为Form_frmCustomers
        Set frm = Forms!frmCustomers.OrderItems.Form
        ‘注意别写成了Set frm = Forms!frmCustomers.frmOrderItems_sub.Form  
        当你需要从一个窗体引用另一个窗体时我鼓励你们使用窗体变量在代码中。这是ACCESS中一个绝佳的工具,可以在你的应用程序使访问共享信息更容易,甚至能加快它的运行速度。

【原文】

Use the Form property when referencing subforms in your code

Form variables can make your code sizzle

I love using form variables. They make my code faster and easier to read, and they also provide a great way to remote control another form without being on that form or having it be the focus. Another advantage is that reading record values off a form is far quicker than using a recordset using a table off the disk. If you know the data you’re looking for is already loaded on a form, reading it by using a form variable can speed up your application.

For example, let’s assume I’m coding in a form called frmOrders and I need to read controls on an open form called frmCustomers (you can only associate a form variable to an open form). I would use the following code to instantiate my variables:

Dim frm as Form_frmCustomers
Dim lngCustomerID as Long   
If CurrentProject.AllForms("frmCustomers").IsLoaded then
Set frm = Forms!frmCustomers
'Use the frm variable to read fields off the form

lngCustomerID = frm.CustomerID
Else
'Use another technique to find CustomerID

End If


One of the advantages of declaring frm as Form_frmCustomers instead of as the generic Form type is that IntelliSense will pop up to help you refer to controls on the form. You can do this with any form that's already loaded; just make sure you use "Form_" followed by the form name when declaring variables of this type.

Subforms work too, but you need to use the Form Property

Subforms are considered part of the main form and lose their own identity once they are loaded. In order to reference a subform, you need to use the Form property to associate the form variable with the subform. Let's assume frmOrders is the parent form and frmOrderItems_sub is the name of the subform inserted into frmOrders. If you need to reference the subform, you could do so in the following matter:

Dim frm as Form_frmOrderItems_sub
Set frm = Forms!frmCustomers.frmOrderItems_sub.Form


Notice the reference to the .Form property at the end of the second statement, which is needed in order to associate the subform with the variable frm.

Note 1: You can only use form variables on forms that have modules; otherwise, the compiler will error out.                                                            Note 2: Make sure you reference the correct name of the subform on the main form or the code will not work. For example, if the subform control has a SourceObject set to a form named "frmOrderItems_sub" but has a control name of "OrderItems" on the main form, then your code should look like this:

Dim frm as Form_frmOrderItems_sub                                                                                                                                                                                           Set frm = Forms!frmCustomers.OrderItems.Form

I encourage you to start using form variables in your code when you need to reference one form from another. It's another great tool in Access that will make sharing information easier across your application and can even speed it up.



Access软件网QQ交流群 (群号:483923997)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助