A great deal of your code will reference Access objectsforms and controls for the most part. Knowing how to reference objects and data correctly is one of the basics you need to master. You can start by learning a few new terms:
IdentifierAn expression that identifies the value of a control, property, or another expression.
OperatorWithin this context, an operator is a symbol used to separate an identifier's individual components. An identifier can have several layers. There are two identifier operators: the dot and the bangthe period and exclamation characters, respectively. You read about just the bang operator in this section.
QualifierIdentifies an object's collection.
Learn more about the dot operator in "Reading and Setting Properties," in Chapter 8.
When referencing objects, VBA needs to know not only what object to reference but also what kind of object it is. At this point, TimeTrack.mdb (the sample database you customize throughout this book) has several objectstables, forms, reports, and even a VBA module. In addition, more than 60 controls comprise the application's forms and reports. There are a lot of possible references.
To reference any of these objects, use the form
where qualifier identifies the object's collection and objectname identifies the object. Notice that the bang character (!) separates the two components. For instance, to refer to the Clients form in TimeTrack.mdb, you use the following expression
Forms refers to the form collection, and Clients is the name of the form. To reference the billing report, you use the expression
Controls are a little different because a control belongs to the form or report (the parent object). That means you have to work through two layers of objects (identifiers in this case) using the form
where qualifier is either Forms or Reports.
Controls display data retrieved from a field in a specific table or query. The term field refers to the column in the table or query where the data is stored. A control is an object that displays that data in a form or report. Often, you will see controls that share the same name as the underlying field that supplies the data, which can cause confusion. Most developers try to differentiate between the two elements by using a prefix tag in the control's name. As a result, txtFirstName becomes the name of a control that displays data stored in a field named FirstName.
To illustrate this syntax, look at the Employees form shown in Figure 3.8. This form has three controls, EmployeeID, FirstName, and LastName. Each control's full name contains a qualifier, the form's identifier, and the control's identifier:
Forms![Employees]![EmployeeID] Forms![Employees]![FirstName] Forms![Employees]![LastName]
All three controls belong to the Controls collection for this form, which is the form object's default collection. Consequently, you can omit the Controls collection reference.
Figure 3.8 A collection of controls belongs to the form or report.
You probably noticed that each object is enclosed in brackets ([ ]). The brackets keep VBA from returning an error if the name contains a space character. When there's no space character, you can omit the brackets. However, if you forget about the rule and omit the brackets when the name does contain spaces, VBA will return an error. That's why many developers include them out of habit rather than needjust in case. The best solution is to omit space characters in object names.