You don't always have to refer to an object by its specific name. You can use some special names in code to refer to objects in Access, as listed here:
1 Me: In a class module, the term Me refers to the form or report to which the class module is attached. For example, Me![Selling Price] is short for "the control named Selling Price on the form to which this code is attached."
1 CodeContextObject: This refers to the name of the object in which the code is running. (In a class module, it's always the same as the form or report to which the class module is attached.)
1 Screen.ActiveControl: This refers to whatever control has the focus right now.
Each of the preceding names supports a Name property, which you can use to determine the name of the control. For example, take a look at the sample form named MyForm in Figure 8-4. Note the names of the controls on the form.
The class module shown in Figure 8-4 is the class module for that tiny MyForm form in the same figure. Note the use of various names in the code. Here's what each of those names returns when the module is run:
i Me.Name: Displays MyForm because MyForm is the name of the form to which the module is attached.
i Me.Controls.Count: Displays 3 because there are three controls on
• MyCombo label
• MyCombo combo box
• MyBttn button
Note that Me.Controls refers to the current form's Controls collection. The .Count property returns the number of items in the collection.
i Me!MyCombo.Value: Displays Artichoke, which is the value of the control named MyCombo on the current form.
i CodeContext.Name: Returns MyForm in this example because the class module always runs within the context of the current form, whose name in this case is MyForm.
i Screen.ActiveControl.Name: When executed in this example, returns MyBttn because the user clicks MyBttn to execute the code. MyBttn gets the focus when the user clicks it.
Screen.ActiveControl.Value returns whatever value is stored in whatever control on-screen now has the focus. Be careful when you're using
Screen.ActiveControl.Value because not every control has a Value property.
Was this article helpful?