referencing a subform by its name in a string

B

brm6546545

if i have to strings

Dim subFormName as string
Dim formName as string.

'this works
Forms(formName).txtOnMainForm = "hello"
'these do not
Forms(formName).Forms(subFormName).txtOnSubForm = "hello"
Forms(formName).SubForm(subFormName).txtOnSubForm = "hello"
Forms(formName & "!" & subFormName).txtOnSubForm = "hello"

Any way to reference a subform if its name is in a string?
 
K

kingston via AccessMonster.com

I'm not sure that I understand your variable assignments, but in general it
is a bad idea to give two objects the same name (e.g. a variable and a form
are both called XYZ). Now let's assume that you have the following:

MainForm
SubForm
TextControl

To reference TextControl, use Forms!MainForm!SubForm!TextControl = "Whatever".

Note that the name of a subform is not necessarily the name of the subform
control in a main form. You have to use the subform control's name.
 
B

brm6546545

Sorry i should have been more clear. the name of the form and subform
are dynamic. the name of the form is stored in the string formName.
"formName" is not the name of the form.

'so if
formName = "frmInventory"
'this
Forms(formName).txtOnMainForm = "hello"
'is the same as this
Forms("frmInventory").txtOnMainForm = "hello"

the catch is i can't hard code the subform name, i have to use what is
int the variable.

I know how to reference a control on a form this way, but not a
subform.
 
K

kingston via AccessMonster.com

Assuming the subform control name is passed to you correctly (subform names
are not necessarily equal to subform control names), you might be able to
find the index of the subform control by looping through all of the form's
controls. Then you might be able to use the index number in your code. HTH.
Sorry i should have been more clear. the name of the form and subform
are dynamic. the name of the form is stored in the string formName.
"formName" is not the name of the form.

'so if
formName = "frmInventory"
'this
Forms(formName).txtOnMainForm = "hello"
'is the same as this
Forms("frmInventory").txtOnMainForm = "hello"

the catch is i can't hard code the subform name, i have to use what is
int the variable.

I know how to reference a control on a form this way, but not a
subform.
I'm not sure that I understand your variable assignments, but in general it
is a bad idea to give two objects the same name (e.g. a variable and a form
[quoted text clipped - 26 lines]
Message posted via AccessMonster.com
 
J

John Vinson

I know how to reference a control on a form this way, but not a
subform.

It's confusing. A Subform IS A CONTROL; you can reference it using

Me.Controls("subMySubform")

or

Me.Controls(strSubformname)

What's confusing is that the Form object *within* that subform control
need not have the same name as the name of the Control; in fact the
name of the Form being used as the subform's SourceObject is
irrelevant to the referencing. If you want to display different forms
within a subform control, just change the control's SourceObject
property; you could then use

Me!subMySubform.Form!controlname

or

Me!subMySubform.Form(strControlname)

to reference a control on the subform, without even needing to know
the Name property of the Form that you're currently using.

John W. Vinson[MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top