简体   繁体   English

如何在MS Access中引用子表单

[英]How to reference a subform in MS Access

In my MS Access application, I am using a form which contains only two controls - a textbox and a command button. 在我的MS Access应用程序中,我使用的表单只包含两个控件 - 文本框和命令按钮。 This form is named as HEADER FORM . 此表单被命名为HEADER FORM

HEADER FORM is used as a subform in header section of various other forms. HEADER FORM用作各种其他形式的标题部分中的子表单。

What I want to do is that whenever a particular form loads, I want to fill details in the textbox of the HEADER FORM (that will be name of the person who has logged in. The same would be clear from the picture below). 我想要做的是,每当一个特定的表单加载时,我想填写HEADER FORM的文本框中的细节(这将是已登录的人的名字。从下图中可以清楚地看到相同的内容)。

I am trying to call a global subroutine named updateHeader in form load event of all the forms. 我试图在所有表单的表单加载事件中调用名为updateHeader的全局子例程。

Public Sub updateHeader()
    Me![HEADER FORM].Form.txtHeaderName.Value = strPerson
End Sub

Following is the picture showing HEADER FORM in Design View and the same being used as a subform in a login form. 以下是在设计视图中显示HEADER FORM的图片,同样用作登录表单中的子表单。

在此输入图像描述

I tried various other options but am not able to come out with the correct way to reference the form. 我尝试了各种其他选项,但我无法提出正确的方式来引用表单。 Am I doing something wrong fundamentally? 我从根本上做错了吗?

The error that I am seeing is invalid use of Me keyword. 我看到的错误是无效使用Me关键字。 Also, my updateHeader subroutine is a global subroutin which is called from Form_Load event of all the forms. 另外,我的updateHeader子例程是一个全局子程序,它从所有表单的Form_Load事件中调用。

If your updateHeader() procedure is contained in a standard module, that would explain the complaint about the Me keyword ... it's not valid in a standard module. 如果您的updateHeader()过程包含在标准模块中,那么可以解释有关Me关键字的投诉...它在标准模块中无效。

In a form module, Me means "this form". 在表单模块中, Me表示“此表单”。

You could change the procedure declaration to accept a reference to a form. 您可以更改过程声明以接受对表单的引用。

Public Sub updateHeader(ByRef TheForm As Form)
    ' Me![HEADER FORM].Form.txtHeaderName.Value = strPerson
    TheForm![HEADER FORM].Form.txtHeaderName = strPerson
End Sub

.Value is the default property and therefore not needed here, so I left it out. .Value是默认属性,因此这里不需要,所以我把它留了出来。 But it won't hurt to add it back if you prefer. 但如果您愿意,可以将其添加回来。

You can then call the procedure from the parent form, and pass the procedure a reference to itself (the parent form). 然后,您可以从父窗体调用该过程,并将该过程传递给它自己(父窗体)。

updateHeader Me

I got these "syntax versions" from Wiley.Microsoft.Office.Access.2007.Bible: When referencing subform controls: 我从Wiley.Microsoft.Office.Access.2007获得了这些“语法版本”.Bible:引用子窗体控件时:

Forms![FormName]![SubformName].Form![ControlName] 窗体![窗体名称]![SubformName] .FORM![控件名称]

When using/referencing subforms within subforms, use the following syntax: Forms![FormName]![SubformName].Form![SubSubformName].Form.[ControlName] 在子表单中使用/引用子表单时 ,请使用以下语法: Forms![FormName]![SubformName] .Form![SubSubformName] .Form。[ControlName]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM