简体   繁体   中英

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. This form is named as HEADER FORM .

HEADER FORM is used as a subform in header section of various other forms.

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).

I am trying to call a global subroutine named updateHeader in form load event of all the forms.

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.

在此输入图像描述

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

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.

In a form module, Me means "this form".

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. 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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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