Referring to a subform from another subform in VBA (1 Viewer)

cowenpa

Member
Local time
Today, 18:15
Joined
Apr 7, 2021
Messages
38
This is driving me potty - a couple of times I have had it where can debug past a line so I think it's working then later throws up an error on same code.

I have 3 forms currently
1621435530130.png

When I Select something on the first one I want the bottom subform to update so I have code on on current and tried about a bazillion ways of getting it to work and nothing seems to work and allow the following code to run which works fine elsewhere


Private Sub Form_Current()
Set frm2 = Me.Parent![FrmForecastDeptForm]
For Each ctrl In frm2
If ctrl.Tag = "Hide" Then ctrl.ColumnHidden = True
Next ctrl

The Parent form is called MonthlyDeptTest but if I use that it says not recognized. The top subform is called FrmMonthDeptQuery and the bottom one is called FrmForecastDeptForm with source object ForecastDeptForm

If I try putting Me.Parent![FrmForecastDeptForm].Form I get
1621435749769.png

If I leave out form then it continues but then errors with
1621435789179.png

Me.Parent.Controls![FrmForecastDeptForm].Requery runs fine

Have tried Me.Parent.Controls![FrmForecastDeptForm] and Me.Parent.Controls![FrmForecastDeptForm].Form and neither of those work

If I try something including Forms!["MonthlyDeptTest"] I get error:
1621435885906.png


I have tried googling and still stuck so on here asking for help before I go spare. What was really annoying was when it worked with one combination and I thought had solved it and then later go to run same code and it no longer ran and sprung up an error - grrrrr

Thanks Experts

Paul
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:15
Joined
Oct 29, 2018
Messages
21,358
Hi. Things like this is easier when we can actually see the file. Not even sure you need any code. The easiest way to manipulate a subform is by simply using the Linked Master/Child Fields properties. If you must use an expression, there are two ways to go: absolute or relative path. Either way, you need to use the name of the subform control/container and not the name of the form it contains.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:15
Joined
Feb 28, 2001
Messages
26,999
I think part of the problem is that you must refer to a subform by the name of the subform's control.

So from subform1's form, ... Me.Parent.SubForm2.Form.SpecificControlName would get you from the form in subform1 to a control on the form in subform2. You don't ever actually refer to the name of the subform if you are using this kind of syntax.
 

conception_native_0123

Well-known member
Local time
Today, 13:15
Joined
Mar 13, 2021
Messages
1,826
I have all of this information stored from years ago, just based on the title of this thread. Perhaps it will help too...

REFERENCE SUBFORM FROM PARENT FORM
Me.subFormName.Form.Action

REFERENCE SUBFORM CONTROL FROM PARENT FORM
Me.subFormName.Form!ControlName.Action

REFERENCE SUBFORM CONTROL FROM A POPUP FORM
Forms!MainFormName!SubformControlName.Controls("ControlName").Action

REFERENCE SUBFORM'S SUBFORM CONTROL
Me.subForm2Name.Form.ControlName.Action

REFERENCE SUBFORM of a SUBFORM FROM PARENT FORM (2 NESTED SUBFORMS DEEP)
Me.subForm1Name.Form!subForm2Name.Action

REFERENCE SUBFORM of a SUBFORM CONTROL FROM PARENT FORM (A CONTROL 2 NESTED SUBFORMS DEEP)
Me.subForm1Name.Form!subForm2Name.Form!ControlName.Action

REFERENCE PARENT FORM FROM SUBFORM
Me.Parent.Action

REFERENCE PARENT CONTROL FORM FROM SUBFORM
Me.Parent.ControlName.Action

REFERENCE MAIN FORM FROM SUBFORM 2 LEVELS DEEP
Forms(Me.Parent.Parent.Name).Action

REFERENCE MAIN FORM CONTROL FROM SUBFORM 2 LEVELS DEEP
Forms(Me.Parent.Parent.Name).ControlName.Action

Note: The code in italics indicates object and control names that you have to include.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 14:15
Joined
May 21, 2018
Messages
8,463

cowenpa

Member
Local time
Today, 18:15
Joined
Apr 7, 2021
Messages
38
Although it's not a solution to your specific problem, bookmark this page in your browser : http://access.mvps.org/access/forms/frm0031.htm

It is the clearest explanation of how and when to refer to other objects I have seen.
Although it's not a solution to your specific problem, bookmark this page in your browser : http://access.mvps.org/access/forms/frm0031.htm

It is the clearest explanation of how and when to refer to other objects I have seen.
I already had found this page when trying to resolve it myself - yes seems fairly comprehensive but still struggling.
 

cowenpa

Member
Local time
Today, 18:15
Joined
Apr 7, 2021
Messages
38
FYI,
If you read the OPs other thread this may be an issue with corruption and not syntax
Run time error on code that was previously running fine | Access World Forums (access-programmers.co.uk)
The OP seems to have the correct syntax, if what they state is accurate, and it failed
Me.Parent![FrmForecastDeptForm].Form
Based on the other thread any reference to the form object is failing.
I recreated the database after corruption and this is on the recreated database so don't think it is same issue - just probably slightly messing up the syntax but driving me potty
 

CedarTree

Registered User.
Local time
Today, 14:15
Joined
Mar 2, 2018
Messages
404
Thanks. BTW if your routine to refer to another form is on Form_Current, sometimes that runs before the other subform loads, so you have to error trap that.
 

Users who are viewing this thread

Top Bottom