How do I reference a subform?

K

Kan D.

How do I programmatically reference a subform?

This does NOT work:

Public Sub CompareStrings(FormName As String, FormField As String)
Dim ThisForm As SubForm
Set ThisForm = Forms.(FormName)

I get an error message:
Microsoft Office Access can't find the form 'My Form Name' referred to in a
macro expression or Visual Basic Code.

Thanks for your help,

Kan
 
G

Guest

Kan.... very closely double check the spelling of the form names. It may be
as simple as having characters that are not recognized.
 
A

Allen Browne

That's right. Subforms are not open in their own right, i.e. they are not
part of the Forms collection.

See:
Referring to Controls on a Subform
at:
http://allenbrowne.com/casu-04.html
for the general syntax, and the difference between a subform control and the
form in that control.

As far as your code goes, could you just pass the entire control and/or form
reference to your function?

Public Sub CompareStrings(frm As Form, ctl As Control)
Debug.Print frm.Name
Debug.Print ctl.Name, ctl.Value
...

You would then call it like this, regardless of whether you are in a main
form's or a subform's module:
Call CompareStrings(Me, Me.Text0)
Or, if you are in the main form's module and want to pass a reference to the
subform:
Call CompareStrings(Me.[Sub1].Form, Me.[Sub1].Form!Text0)
 
K

Kan D.

Kristy,

I'm using a variable to pass the form name, and I'm passing the form name
using:

Me.Name (Which is a subform).

So there's no problem with the form name.

Still have a problem, what next?

Kan
 

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