Referencing Syntax

 < Day Day Up > 

A great deal of your code will reference Access objects forms 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:

  • Identifier An expression that identifies the value of a control, property, or another expression.

  • Operator Within 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 bang the period and exclamation characters, respectively. You read about just the bang operator in this section.

  • Qualifier Identifies an object's collection.

Learn more about the dot operator in "Reading and Setting Properties," in Chapter 8 (p. 116).

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 objects tables, 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] 

Figure 3.8. A collection of controls belongs to the form or report.


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.


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 need just in case. The best solution is to omit space characters in object names.

     < Day Day Up > 

    Automating Microsoft Access with VBA
    Automating Microsoft Access with VBA
    ISBN: 0789732440
    EAN: 2147483647
    Year: 2003
    Pages: 186 © 2008-2017.
    If you may any questions please contact us: