VBA - what is the difference between using ! and a period / dot?

I

ITMA

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?
 
S

Steve Jorgensen

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.
 

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