How do I refer to a control in a subform, nested in another subform? (1 Viewer)

pityocamptes

Registered User.
Local time
Today, 06:28
Joined
Dec 8, 2010
Messages
27
I basically have a main form (A), with a subform (B) on the main form. I also have a subform (C) nested inside subform (B). How do I access a control in subform (C)??? I have tried all sorts of combinations and I keep getting VB errors in Access. Any help appreciated. Thanks!!!
 

boblarson

Smeghead
Local time
Today, 06:28
Joined
Jan 12, 2001
Messages
32,059
You might want to read this from my website.

Also, there are a few different ways to do this but I have found this to be the easiest to remember (especially for nested subforms):
Code:
Forms("YourMainFormNameHere").Controls("YourSubformControlHere").Form.Controls("YourSecondSubformnameHere").Form.Controls("YourControlOnSecondSubform).Value
And YourSubformControlHere means the name of the subform Control which HOUSES the subform on the main form or on the other subform. Not the name of the subform itself unless both are named exactly the same (subform and subform control).
 

pityocamptes

Registered User.
Local time
Today, 06:28
Joined
Dec 8, 2010
Messages
27
You might want to read this from my website.

Also, there are a few different ways to do this but I have found this to be the easiest to remember (especially for nested subforms):
Code:
Forms("YourMainFormNameHere").Controls("YourSubformControlHere").Form.Controls("YourSecondSubformnameHere").Form.Controls("YourControlOnSecondSubform).Value
And YourSubformControlHere means the name of the subform Control which HOUSES the subform on the main form or on the other subform. Not the name of the subform itself unless both are named exactly the same (subform and subform control).


Thanks! I will try this. Do I use the () and "" in the code? ("YourSecondSubformnameHere").
 

pityocamptes

Registered User.
Local time
Today, 06:28
Joined
Dec 8, 2010
Messages
27
For some reason this is not working. It keeps saying it can't find the subform. Perhaps I am mistaken in the nesting, though I do not think so. How would I know for sure? Thanks!
 

boblarson

Smeghead
Local time
Today, 06:28
Joined
Jan 12, 2001
Messages
32,059
For some reason this is not working. It keeps saying it can't find the subform. Perhaps I am mistaken in the nesting, though I do not think so. How would I know for sure? Thanks!
Read the article I posted and look at the screenshot in the article. It should help you figure it out. Post back if it doesn't but first try.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 14:28
Joined
Jul 9, 2003
Messages
16,393
First thing is to get out of your head the idea that the main form carries a sub-form and so on.

Sub-form is a misleading term, as your “sub-form” is not actually sub, below, the main-form; it actually resides in what is called a “sub form window”.
The first issue is that MS Access with its usual disregard for naming conventions, names this “sub form window” the same name as the form contained within it, obviously something which will lead you up the garden path! So forget about the term “sub-form” when you are thinking of how to access a “sub-form” on a main form.

Your main form has on it a “sub form window” in this subform window you have a form. To access a control on the form contained within the “sub form window”, you use code similar to similar to this:

SubformWindow.Form.Control
(Note Form here is a directive, it says: Work with the form in the sub form window, and now, this particular Control on the Form)

If you want to access something on the ”second level” sub-form, in your case “FormC”, then you first have access a control on “FormA” a sub-form window on form “FormA”, then the form in it “FormB”, then a control on “formB” again a sub form window which contains “FormC”.

To access a text box on your FormC called myTxtBox you would use code something akin to this:

Assumption:
You have a form “FormC” which is on “FormB” which is on “FormA”

Call the following code from a Command button on “FormA”
MsgBox “ >>> “ & FormB.Form.FormC.Form.myTxtBox
 

ChrisO

Registered User.
Local time
Today, 23:28
Joined
Apr 30, 2003
Messages
3,202
The OP has specified the destination quite clearly but has not stated the starting point.

Chris.
 

Dairy Farmer

Registered User.
Local time
Today, 16:28
Joined
Sep 23, 2010
Messages
244
Ok, here is a little trick that works quite well.

Open the form that contains the nested forms in design view.
Now create a new blank query. No need to add any tables.
Now go to the field and select Build wizard.
In the Expression Builder select Forms then Loaded Forms.
Keep drilling down untill you find the field you with to refer to ans select it.
You now have the correct syntex for that field.
 

vbaInet

AWF VIP
Local time
Today, 14:28
Joined
Jan 22, 2010
Messages
26,374
...
Now create a new blank query. No need to add any tables.
Now go to the field and select Build wizard.
...
Or a blank form with a textbox.
Go to the Control Source of the textbox and click the elipsis button, select Expression Builder and OK that.
 

boblarson

Smeghead
Local time
Today, 06:28
Joined
Jan 12, 2001
Messages
32,059
Ok, here is a little trick that works quite well.

Open the form that contains the nested forms in design view.
Now create a new blank query. No need to add any tables.
Now go to the field and select Build wizard.
In the Expression Builder select Forms then Loaded Forms.
Keep drilling down untill you find the field you with to refer to ans select it.
You now have the correct syntex for that field.

Here's the same explanation but with pictures.
 

Users who are viewing this thread

Top Bottom