Understanding the Object Model

  • Thread starter TraciAnn via AccessMonster.com
  • Start date
T

TraciAnn via AccessMonster.com

Working with forms, I constantly find it difficult to "locate" an object
using vba. Can someone help me understanding the "path" to certain objects?

For example:
A Main Form (frmMain) with a Tabbed control (tabControl) with Subforms
(frmSub1, frmSub2, etc.) on each tab.

When I am in a control (txtControl1) on frmSub1 and I want to reference a
value in a control (txtControl2) on frmSub2, I try typing a variety of
"paths" in my code editor but I can't find the right combination. I have
tried using page control names (pgeControl1, pgeControl2, etc.) also but it
still doesn't work.

Logically it seems that it should be:
Me.Parent.tabControl.pgeControl2.frmSub2.txtControl2
OR
Forms.frmMain.tabControl.pgeControl2.frmSub2.txtControl2

Please help me understand this! Thanks!
 
K

Klatuu

All you need is

Forms!MainFormName!SubFormControlName!Form!ControlName

Note that the SubFormControlName is NOT the name of the form being used as a
subform. It is the name of the subform control on the main form. It is the
Source Object property of the subform control that binds the subform to the
control.
 
K

Ken Snell [MVP]

Tab controls *do not* participate in the "path" for referencing controls on
a form. They are not "containers" the way a subform is. So you ignore a tab
control when referencing a control on that tab page. For example, if you
have TextBox1 on page 1 of the tabControl, and TextBox2 on page 5 of the
tabControl, you reference them this way from the main form:

Me.TextBox1
Me.TextBox2


From a subform on the main form, you'd use

Me.Parent.TextBox1
Me.Parent.TextBox2


Thus, using your posted example:

Me.Parent.frmSub2.Form.txtControl2

or

Me.Parent!frmSub2!txtControl2


The above assumes that the name of the subform control on the main form (the
control that holds the subform object) is named frmSub2. Note that the name
of the subform control may or may not be the same name as its Source Object
(which is the actual name of the form that is being used as the subform).
 
T

TraciAnn via AccessMonster.com

Me.Parent.frmSub2.Form.txtControl2
Me.Parent!frmSub2!txtControl2

The above assumes that the name of the subform control on the main form (the
control that holds the subform object) is named frmSub2. Note that the name
of the subform control may or may not be the same name as its Source Object
(which is the actual name of the form that is being used as the subform).

Okay. I understand everything you said, including the difference between the
subform control and the source object name. As a result, I'm still hung up on
a couple things.

1. your samples above point to the exact same control but the first, using
period separators, includes "Form" whereas the second, using exclamation
separators, excludes "Form". Why the difference?

2. It appears that the name of my subform control and the source object name
is always the same. I don't do it intentionally, I simply follow a naming
procedure for all my objects. Is there a way to tell for sure the names of
each?

When in design mode I click once on the subform of which the name appears in
the Property sheet Selection Type combobox. The Data tab "Source Object" and
the Other tab "Name" have the same value. I assume the value in "Source
Object" is the name of the Form (as listed in Access Objects) and "Name" is
the name of the control, or the container that is holding the form.

If this is the case and understanding the answers are purely subjective, am I
adhering to good development practices? is there a better way?

Thanks Ken and Klatuu for your time!
 
T

TraciAnn via AccessMonster.com

Damon and Piet,

Thank you! Good idea! It's hanging on my cube now!

Although I've referenced this before, I was still a little baffled by the Tab
control and Page Control, thinking that this document did not take into
consideration that a person is using the tab control. So I was trying to put
one or both of the controls in my references.

It sure does help to have this newsgroup as a resource!

Thanks Again!
 
K

Ken Snell [MVP]

Answers inline...
--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/


TraciAnn via AccessMonster.com said:
Okay. I understand everything you said, including the difference between
the
subform control and the source object name. As a result, I'm still hung up
on
a couple things.

1. your samples above point to the exact same control but the first, using
period separators, includes "Form" whereas the second, using exclamation
separators, excludes "Form". Why the difference?

Using the . syntax means that the syntax is referring to properties of the
form. In order for you to refer to a property of the subform form itself
(the form that is the subform), one needs to use the .Form property of the
subform control.

However, if you use the ! syntax, then you're referring to the default
collection of the preceding object, which for form's and subform control's
is the Controls collection. Therefore, when you use ! after the subform
control, it means to refer to the Controls collection of the form that is
the subform, and there is no need to reference the control on the subform
through the Form property.


2. It appears that the name of my subform control and the source object
name
is always the same. I don't do it intentionally, I simply follow a naming
procedure for all my objects. Is there a way to tell for sure the names of
each?

The SourceObject property of the subform control will be the actual name of
the form that is being used as the subform. The Name property of the subform
control is the name of the subform control. ACCESS will default the two
properties to be the same when you drag a form (to be the subform) onto a
form in design view. It's ok if they're the same name so long as you
remember that they are not the same property.
 
D

David W. Fenton

Tab controls *do not* participate in the "path" for referencing
controls on a form. They are not "containers" the way a subform
is. So you ignore a tab control when referencing a control on that
tab page.

You bypass the tab control, but it is also the case that the tab
control has its own collections, e.g., the Pages collection, and
each page has a Controls collection.

Indeed, there are Controls collections for almost everything. For
instance, this is a reference that is valid:

Me!txtMyTextBox.Controls(0).Caption = "Label for textbox"

This is a way to set the caption of a label attached to a textbox
without needing to know the name of the label itself, as the
attached label is the only item in the textbox's controls
collection.
 
D

David W. Fenton

if you use the ! syntax, then you're referring to the default
collection of the preceding object, which for form's and subform
control's is the Controls collection.

Actually, it's a combination of the Controls and Fields collections
of the forms.

Highly confusing, indeed.
 

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