Category talk:VBA

From database24
Jump to navigation Jump to search

! vs .

http://bytes.com/topic/access/insights/620587-control-object-reference-me

You’ve probably noticed that a complex, fully qualified name of an object or a property in Access or Visual Basic contains exclamation points (!) and periods (.) that separate the parts of the name.

Use an exclamation point preceding a name when the name refers to an object that is in the preceding object or collection of objects. A name following an exclamation point is generally the name of an object you created (such as a form or a table). Names following an exclamation point must be enclosed in brackets ([ ]) if they contain embedded blank spaces or a special character, such as an underscore (_). You must also enclose the name of an object you created in brackets if the name is the same as an Access or SQL reserved word. For example, most objects have a Name property—if you name a control or field “Name,” you must use brackets when you reference your object.

To make this distinction clear, you might want to get into the habit of always enclosing in brackets names that follow an exclamation point, even though brackets are not required for names that don’t use blank spaces or special characters. Access automatically inserts brackets around names in property sheets, design grids, and action arguments.

Use a period preceding a name that refers to a collection name, a property name, or the name of a method that you can perform against the preceding object. (Names following a period should never contain blank spaces.) In other words, use a period when the following name is of the preceding name (as in the TableDefs collection of the Databases(0) object, the Count property of the TableDefs collection, or the MoveLast method of the DAO Recordset object).

This distinction is particularly important when referencing something that has the same name as the name of a property. For example, the reference

DBEngine.Workspaces(0).Databases(0).TableDefs(13). Name

refers to the name of the fourteenth TableDef object in the current database. In the Contacts .mdb database, if you use Debug.Print to display this reference, Visual Basic returns the value tblCompanyContacts. However, the reference

DBEngine.Workspaces(0).Databases(0).TableDefs(13)![Name]

refers to the contents of a field called Name (if one exists) in the fourteenth TableDef object in the current database. In the LawTrack Contacts database, this reference returns an error because there is no Name field in the tblCompanyContacts table.