Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

like image 575
Jay Avatar asked Oct 23 '22 03:10

Jay


1 Answers

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
like image 96
HansUp Avatar answered Nov 01 '22 12:11

HansUp