Referring to Objects
Access objects are categorized into collections , which are groupings of objects of the same type. The Forms collection, for example, is a grouping of all the open forms in a database. Each form has a Controls collection that includes all the controls on that form. Each control is an object, and you must refer to an object through the collection to which it belongs. For example, you refer to a form through the Forms collection. VBA offers three ways to refer to an object; if you want to refer to the frmProjects form, for example, you can choose from the following options:
- Forms.frmProjects (or Forms!frmProjects)
- Forms("frmProjects")
- Forms(0)
Referring to the form as Forms(0) assumes that frmProjects was the first form opened. However, you need to understand that although Access assigns an element number as it loads each form , this element number changes as Access loads and unloads forms at runtime. For example, the third form that's loaded can initially be referred to as element two, but if the second form is unloaded, that third form becomes element one. In other words, you can't rely on the element number assigned to a form; that number is a moving target.You must refer to a control on a form first through the Forms collection and then through the specific form. The reference looks like this:Forms.frmProjects.txtClientID
In this example, Forms is the name of the collection, frmProjects is the name of the specific form, and txtClientID is the name of a control on the frmProjects form. If this code is found in the Code module of frmProjects, it could be rewritten like this:Me.txtClientID
Me refers to the current form or report. It's generic because the code could be copied to any form having a txtClientID control, and it would still run properly. Referring to a control on a report is very similar to referring to a control on a form. Here's an example:Reports.rptTimeSheet.txtHoursBilled
This example refers to the txtHoursBilled text box on the rptTimeSheet report, part of the Reports collection. After you know how to refer to an object, you're ready to write code that modifies its properties and executes its methods.