Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you use a string to address a subform 1

Status
Not open for further replies.

ccoindre

Programmer
Jun 29, 2001
18
US
It is easy to create a string representing a form name and use it to address a Form using the format Forms("formname"), where a string (e.g. strFormname) can be constructed elsewhere and substituted as Forms(strFormname). However, there is no construct to address a subform in the same manner. The format is
Forms("formname").SubFormName.etc. Is there a way to use a string constructed elsewhere representing SubFormName to address a subform and change the subforms properties?

Thanking you in advance
 
Hmm, maybe something like this? Suppose you are using combo boxes to choose the form and subform, and so we'll use the names cmbForm and cmbSubForm as the names of the combo boxes in the example, then it would be something like this:

Also, a button, called cmdClickMe:

Code:
Private Sub cmdClickMe_Click()
  Dim strForm As String [green]'string for holding name of Form[/green]
  Dim strSubForm As String [green]'string for holding name of SubForm[/green]
  Dim strAddress As String [green]'holds string for the context for the subform[/green]

  strForm = cmbForm
  strSubForm = cmbSubForm
  strAddress = Forms(strForm).strSubForm

 [green] 'Then you could call it like this, I would assume:
  [/green]
  strAddress.Visible = True
  strAddress.cmbSomeSubFormControl.Visible=True
This may or may not work - not tested, but I would think something along these lines should work.

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Thenks kjv1611,
I've tried this before. Any string that represents the name of the subform used after the dot connect in the Forms format results in a "cannot find form" error message; it thinks the string name is the subform name. I need to use this capability in a loop where the subform names are created and changes to the subforms properties are made (like changing the RecordSource table, query, or assigning an SQL statement.
 
Have you tried:

Forms(strFormName).Controls(strSubFormName).Form.<desired form property>

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hmm, is there a FormDefine and/or SubFormDefine in the DAO object class? Because I know you can create tdf objects (table define objects) in the DAO object class. Have you checked into that possibly? Because they you could use that to take the name from such as strAddress into the form define.. I've never tried, it's just a guess.

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Thanks to CajunCenturion,
Tried suggestion got error message stating control not found. I guess it's trying to assign a property to a control and not to the form itself.

... and to kjv611,
I couldn't find info on FormDefine or SubFormDefine, but, I only used MS Access Help.

I'm going to simply hardcode the subform names into the code. Less than elegant, but it works.

Appreciate your help
ccoindre
 
To the main form, a subform is nothing but a control. You may be hung up on the name of the subform control, which may not be the actual name of the subform form.

If you pull up the main form in design, and open the properties window and click on the subform, you will find the property "Source Object" on the Data tab. This corresponds to the subform's actual Form name. However, on the "Other" tab, you will find the "Name" property, and this is the name of the subform control to the main form.

If you use the line:
Forms(strFormName).Controls(
strSubFormName).Form.<desired form property>
then make sure that strSubFormName is the name of the subform control ("Other" tab - "Name" property), and not the name of the subform form.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I just looked at this one again, and that last posting from Cajun was helpful, and seems very logical.

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top