By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,396 Members | 2,501 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,396 IT Pros & Developers. It's quick & easy.

Referring to subform via variable?

P: n/a
I have a sub routine that I'd like to use to get data from a main form
or from a subform preferably using 1 variable. Ex:

If Me.OpenArgs = "SF" Then
strForm = "MF!SF"
Else
strForm = "MF"
Endif
Me.Text1 = Forms(strForm)!Text1

This doesn't work, sadly. I know that I can get the value by getting
Me.Text1 inside of each part of the if statement. Is there a way to do
this or should I just separate the sub into two parts; data from a
single form, data from a subform.

Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Salad wrote:
I have a sub routine that I'd like to use to get data from a main form
or from a subform preferably using 1 variable. Ex:

If Me.OpenArgs = "SF" Then
strForm = "MF!SF"
Else
strForm = "MF"
Endif
Me.Text1 = Forms(strForm)!Text1

This doesn't work, sadly. I know that I can get the value by getting
Me.Text1 inside of each part of the if statement. Is there a way to do
this or should I just separate the sub into two parts; data from a
single form, data from a subform.


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Here we run into the problem of form vs. subform. When a form is a
SourceObject of a subform control it is not considered an open form.
Therefore, referring to a subform as Forms(subFormName)!Text1 will not
work, 'cuz Forms() only refers to open forms.

However, you can set up a form reference to reference the appropriate
form. E.g.:

Dim frm As Form
If Me.OpenArgs = "SF" Then
' This sets the form reference to the sub-form
Set frm = Forms!MF!SF.Form
Else
' This sets the form reference to the main form
Set frm = Forms!MF
Endif
Me!Text1 = frm!Text1

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQIXB64echKqOuFEgEQJ5PACdHmUkG3DtKVIq1XKUcqEGPX rjl+UAoLLy
79LQ5qCE1RezVCMRyFWsBO4u
=w228
-----END PGP SIGNATURE-----

Nov 12 '05 #2

P: n/a
MGFoster wrote:
Salad wrote:
I have a sub routine that I'd like to use to get data from a main form
or from a subform preferably using 1 variable. Ex:

If Me.OpenArgs = "SF" Then
strForm = "MF!SF"
Else
strForm = "MF"
Endif
Me.Text1 = Forms(strForm)!Text1

This doesn't work, sadly. I know that I can get the value by getting
Me.Text1 inside of each part of the if statement. Is there a way to
do this or should I just separate the sub into two parts; data from a
single form, data from a subform.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Here we run into the problem of form vs. subform. When a form is a
SourceObject of a subform control it is not considered an open form.
Therefore, referring to a subform as Forms(subFormName)!Text1 will not
work, 'cuz Forms() only refers to open forms.

However, you can set up a form reference to reference the appropriate
form. E.g.:

Dim frm As Form
If Me.OpenArgs = "SF" Then
' This sets the form reference to the sub-form
Set frm = Forms!MF!SF.Form
Else
' This sets the form reference to the main form
Set frm = Forms!MF
Endif
Me!Text1 = frm!Text1


Mil Gracias, MG.

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.