vba 如何在 MS Access 中引用子表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13244813/
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-11 18:22:05  来源:igfitidea点击:

How to reference a subform in MS Access

vbams-accessms-access-2010subform

提问by Jay

In my MS Access application, I am using a form which contains only two controls - a textbox and a command button. This form is named as HEADER FORM.

在我的 MS Access 应用程序中,我使用的表单只包含两个控件 - 一个文本框和一个命令按钮。这种形式被命名为HEADER FORM

HEADER FORMis 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 updateHeaderin 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 FORMin Design View and the same being used as a subform in a login form.

以下是HEADER FORM在设计视图中显示的图片,并且在登录表单中用作子表单。

enter image description here

在此处输入图片说明

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 Mekeyword. Also, my updateHeadersubroutine is a global subroutin which is called from Form_Load event of all the forms.

我看到的错误是Me关键字的无效使用。此外,我的updateHeader子程序是一个全局子程序,它从所有表单的 Form_Load 事件中调用。

采纳答案by HansUp

If your updateHeader()procedure is contained in a standard module, that would explain the complaint about the Mekeyword ... it's not valid in a standard module.

如果您的updateHeader()过程包含在标准模块中,那将解释有关Me关键字的投诉......它在标准模块中无效。

In a form module, Memeans "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

.Valueis the default property and therefore not needed here, so I left it out. But it won't hurt to add it back if you prefer.

.Value是默认属性,因此这里不需要,所以我省略了它。但是,如果您愿意,将其添加回来也无妨。

You can then call the procedure from the parent form, and pass the procedure a reference to itself (the parent form).

然后,您可以从父窗体调用该过程,并将该过程传递给它自身(父窗体)的引用。

updateHeader Me

回答by Marcus Mackaku

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]

Forms![FormName]![SubformName].Form![ControlName]

When using/referencing subforms within subforms, use the following syntax: Forms![FormName]![SubformName].Form![SubSubformName].Form.[ControlName]

在子窗体中使用/引用子窗体时,请使用以下语法: Forms![FormName]![SubformName].Form![SubSubformName].Form.[ControlName]