Referring to a control on another form

M

Marianne

Hello!

Please somebody explain this to me. Clearly I don't understand some concepts
about how to refer to controls between forms.

I have a form with multiple sub forms.

Main Form
SubForm1
SubForm2
Ho do I correctly refer to a control from Subform2 that is on SubForm1

I am trying to use Before Insert Event to copy the the value from a field on
SubForm1 to a field on SubForm2 each time a new record is added in SubForm2.

This doesn't work:
Me.DetClientID = [Forms]![sfrm_Invoice.Form]![ClientID]

It seems like sometimes it works and sometimes it doesn't.

Please help! Thanks!!!!!

Marianne
 
B

BruceM

You are close, but you need to reference the main form, then the Form
property of the subform control. You have left out the name of the main
form, and you have put the Form property inside the bracket. BTW, the
bracket is needed only if the form name contains a special character, which
is something other than an alphanumeric character or an underscore (spaces
are considered special characters in this context). That being said, the
brackets make it easier to identify objects, controls, and fields when you
are reading the code. Your choice. Try:
Forms![Main Form]![sfrm_Invoice].Form![ControlName]
 
R

ruralguy via AccessMonster.com

Hi Marianne,

Here's a link I often refer people to that you can BookMark:
http://www.mvps.org/access/forms/frm0031.htm
Hello!

Please somebody explain this to me. Clearly I don't understand some concepts
about how to refer to controls between forms.

I have a form with multiple sub forms.

Main Form
SubForm1
SubForm2
Ho do I correctly refer to a control from Subform2 that is on SubForm1

I am trying to use Before Insert Event to copy the the value from a field on
SubForm1 to a field on SubForm2 each time a new record is added in SubForm2.

This doesn't work:
Me.DetClientID = [Forms]![sfrm_Invoice.Form]![ClientID]

It seems like sometimes it works and sometimes it doesn't.

Please help! Thanks!!!!!

Marianne
 
J

John W. Vinson

Hello!

Please somebody explain this to me. Clearly I don't understand some concepts
about how to refer to controls between forms.

I have a form with multiple sub forms.

Main Form
SubForm1
SubForm2
Ho do I correctly refer to a control from Subform2 that is on SubForm1

I am trying to use Before Insert Event to copy the the value from a field on
SubForm1 to a field on SubForm2 each time a new record is added in SubForm2.

This doesn't work:
Me.DetClientID = [Forms]![sfrm_Invoice.Form]![ClientID]

It seems like sometimes it works and sometimes it doesn't.

Well... it seems like a very strange thing to do, and perhaps indicates a
design flaw. If ClientID is the link between the tables bound to subform1 and
subform2, normally one would just use ClientID as the Master Link Field/Child
Link Field (or part of those properties; each link field can consist of a
series of up to ten fieldnames separated by semicolons). No code needed at
all!

The syntax to reference a subform is indeed different. The subform is not open
in its own right and is not part of the Forms collection. From subform2 you
could reference

Parent![ClientID]

or more generally, you could use the Name property of the Subform Control on
the mainform. The name of the form object within that subform is not relevant
(though it will often be the same). Try

Forms!YourMainForm![sfrm_Invoice].Form![ClientID]

Breaking it down... you want the Forms collection; your (open) main form's
name; the name of the Subform control on that form; that object's Form
property, set off with a . rather than a ! since it's a property not a member
of a collection; and finally the name of the control on that form.
 
M

Marianne

That worked! Your Awesome!

BruceM said:
You are close, but you need to reference the main form, then the Form
property of the subform control. You have left out the name of the main
form, and you have put the Form property inside the bracket. BTW, the
bracket is needed only if the form name contains a special character, which
is something other than an alphanumeric character or an underscore (spaces
are considered special characters in this context). That being said, the
brackets make it easier to identify objects, controls, and fields when you
are reading the code. Your choice. Try:
Forms![Main Form]![sfrm_Invoice].Form![ControlName]

Marianne said:
Hello!

Please somebody explain this to me. Clearly I don't understand some
concepts
about how to refer to controls between forms.

I have a form with multiple sub forms.

Main Form
SubForm1
SubForm2
Ho do I correctly refer to a control from Subform2 that is on SubForm1

I am trying to use Before Insert Event to copy the the value from a field
on
SubForm1 to a field on SubForm2 each time a new record is added in
SubForm2.

This doesn't work:
Me.DetClientID = [Forms]![sfrm_Invoice.Form]![ClientID]

It seems like sometimes it works and sometimes it doesn't.

Please help! Thanks!!!!!

Marianne
 
B

BruceM

Glad to help. Elsewhere in this thread ruralguy provided a link to detailed
information about how to reference forms and subforms from various places
within your project. It is a good reference, well worth checking out.

Marianne said:
That worked! Your Awesome!

BruceM said:
You are close, but you need to reference the main form, then the Form
property of the subform control. You have left out the name of the main
form, and you have put the Form property inside the bracket. BTW, the
bracket is needed only if the form name contains a special character,
which
is something other than an alphanumeric character or an underscore
(spaces
are considered special characters in this context). That being said, the
brackets make it easier to identify objects, controls, and fields when
you
are reading the code. Your choice. Try:
Forms![Main Form]![sfrm_Invoice].Form![ControlName]

Marianne said:
Hello!

Please somebody explain this to me. Clearly I don't understand some
concepts
about how to refer to controls between forms.

I have a form with multiple sub forms.

Main Form
SubForm1
SubForm2
Ho do I correctly refer to a control from Subform2 that is on SubForm1

I am trying to use Before Insert Event to copy the the value from a
field
on
SubForm1 to a field on SubForm2 each time a new record is added in
SubForm2.

This doesn't work:
Me.DetClientID = [Forms]![sfrm_Invoice.Form]![ClientID]

It seems like sometimes it works and sometimes it doesn't.

Please help! Thanks!!!!!

Marianne
 

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