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)
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?
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.