I'm trying to learn VBA, particularly for MS Access. Can someone tell me
why sometimes a dot is used and sometimes an exclamation mark to separate
collections / objects / methods? For example, not knowing if it is a
technically correct statement, you might see:
Forms!Form.Requery
why not
Forms.Form.Requery
or even
Forms!Form!Requery
or
Forms.Form!Requery?
The dot operator references a member of the object's class. The ! operator
references a named member of the object's default collection. Sometimes, a
property (but never a method) may be accessible both ways, but only because
the class both exposes the property as a member of the class and provides it
as a member of the default collection. Dot references are always resolved at
compile time if possible, and ! references are never resolved until run-time.
In the case of an Access form, Access creates a hidden class member for each
control in the controls collection which is why you can reference fields using
the dot operator. The same cannot be said in the case of, for instance, a
recordset in which the defailt collection is Fields, and the only items
accessible through that colleciton are field objects.
Incidentally, Access failing to keep the virtual field members of a form in
sync with the controls on the form is a major cause of Access project
corruption, so we've found it to be safer to always use ! in these cases, even
though auto-sense does not work as well that way, and some errors cannot be
caught at compile-time.