Subform reference (1 Viewer)

AndreT

Registered User.
Local time
Yesterday, 16:46
Joined
Mar 16, 2011
Messages
26
I have a subform on a form, although these 2 are NOT linked.

This expression runs well -
Forms!frmMasterSales!sbfUser_Default!chkCustomerContactInvalidDefault=True

But both expressions below produce error '2465'-"Master Sales can't find the field 'sbfUser_Default' referred to in your expression" -
Me!sbfUser_Default.Form!chkCustomerContactInvalidDefault=True
Me!sbfUser_Default!chkCustomerContactInvalidDefault=True

Why? Thanks for your advice in advance.

(sorry I don't know why there is a space in the expression)
 

AndreT

Registered User.
Local time
Yesterday, 16:46
Joined
Mar 16, 2011
Messages
26
That was a silly question. Turn out I wasn't in frmMasterSales but in one of its subform. Replacing Me with Me.Parent solved the problem.

Still the question, both the following works -
Forms!frmMasterSales!sbfUser_Default!chkCustomerContactInvalidDefault
Forms!frmMasterSales!sbfUser_Default.Form!chkCustomerContactInvalidDefault

I do use sbfUser_Default for both control and its form name. Could this be the reason?
 

AndreT

Registered User.
Local time
Yesterday, 16:46
Joined
Mar 16, 2011
Messages
26
Hi, lcook -

That was definitely an excellent reference. Appreciate you help.

Still my question in the original post was why do the 2 different expressions work?

Thanks for your advice.
 

lcook1974

Registered User.
Local time
Yesterday, 19:46
Joined
Dec 21, 2007
Messages
330
I'm not totally sure...I would suspect it has something to with the fact they aren't "linked" to the main form.

This tells Access exactly where to look:
Code:
Forms!frmMasterSales!sbfUser_Default!chkCustomerCo ntactInvalidDefault=True

This is using "Me"..which I would assume "me" is Forms!frmMasterSales:
Code:
Me!sbfUser_Default!chkCustomerContactInvalidDefault
 
Me!sbfUser_Default.Form!chkCustomerContactInvalidDefault

in theory it looks like it should work. But I don't know...just trying to think through it logically.

hopefully someone can give you a better answer.

I'll be watching...cause I want to know too.

Larry
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 09:46
Joined
Jan 20, 2009
Messages
12,863
Still my question in the original post was why do the 2 different expressions work?

Code:
Me.subformcontrolname!somename
The above expression resolves to either of the following in order.
Code:
Me.subformcontrolname.Form!controlname
Me.subformcontrolname.Form.Recordset!fieldname

It was overlooked as a valid reference in that page that everyone links for references, probably because the following does not work.

Code:
Me.subformcontrolname.somename

The use of dot instead of the bang (!) is the difference. One of a small number of places where Access really cares about which operator is used.

This is because the items in the Controls Collection and Fields Collection in the RecordSet of the SourceObject are not manifested as a defacto Property of the subformcontrol.

The confusion with references is extended by the defaults which allow shortened reference to properties and objects in Access. They are not a single default but a sequence.

Most developers know that a simple control such as a textbox or combo has a default property: Value
This allows us to refer to the value implicity via the controlname.

Lesser known is the fact that we use default in a form when we make the familiar references. We expect the following to return the value of a control called somename.
Code:
Me.somename
However in fact we are actually referring to
Code:
Me.Form.Controls!somename.Value
Form.Controls is defaulted just like Value.

Furthemore the Item in the Controls Collection is only the first default. If a control is not found, Access looks for a Field in the Recordset and you get:
Code:
Me.Form.Recordset.Fields!fieldname.Value

This is usually masked by the fact that the Form Wizard reuses the name of the field for the control so all references normally hit the control. With a bound control the point is moot because with a bound control :
Code:
Me.Form.Controls!controlname.Value
has the same value as
Code:
Me.Form.Recordset.Fields!fieldname.Value

However if you give the control and field different names you will see that a field in the recordset can be referred to exactly as if the you were referring to a control so long as no control has that name.

But it is different with subformcontrols which don't really have a default property. However they apparently do have a default object which is the Form used as the SourceObject. So a reference with a bang goes directly to either a control in the Controls collection or a field in the Recordset of the SourceObject.

As it is with the Form, the Controls Collection is the default followed by the Fields Collection in the Recordset.
 

lcook1974

Registered User.
Local time
Yesterday, 19:46
Joined
Dec 21, 2007
Messages
330
Thanks GalaxiomAtHome...I think my head me explode but I get it. sort of...I may have to read it again and again to get it to sink in. :D
 

Users who are viewing this thread

Top Bottom