Special Objects

team lib

There are some properties and objects in Access that we haven't covered yet, but which are quite useful. When changing the font earlier we used the Forms collection to look at all open forms, but what if we wanted this procedure to only work for the active form? Or what if we want to write a procedure where a form object is passed into it? Let's look at these special objects and properties, and see what they do:



Refers to



The control that has the focus.



The data access page that has the focus.



The form that has the focus, or contains the control with the focus.



The report that has the focus, or contains the control with the focus.



The control that had the focus immediately before the control with the current focus.

Form or Report


The current form or report. This is actually a keyword and not a property.

Form or Report


The form or report module.

Form or Report


A copy of the recordset that underlies a form or report. You'll see more of this in the next few chapters.

Subform, form, or the actual control


For a subform control, this is the subform. For a form, it is the form itself. You'll see this in action later in the book.

Subreport, report, or the actual control


For a subreport control, this is the subreport. For a report, it is the report itself.



The form or report that contains the control.



The section of a form or a report upon which a control lies.

Some of these you might not use, but there are some, such as ActiveForm , Me , and RecordsetClone , that you'll use quite often.

Try It Out-The ActiveForm Property

  1. Open the company form, frmCompany .

  2. Switch to the VBE and show the Immediate window.

  3. Type the following:

    ?Screen.ActiveForm. Name

  4. Switch back to Access and open the Switchboard form ( frmSwitchboard ), keeping the company form open too.

  5. Switch back to the VBE and try it again.

So you can see that the ActiveForm object changes, depending upon which form is the currently active one. This would be quite useful if you needed to write a procedure to act upon the current active form. For example, in the FormFonts procedure we created earlier, we used the Forms collection to loop through all open forms, but if we only wanted to act upon the current active form we could do this:

 Dim frmCurrent As Form Dim ctlControl As Control For Each ctlControl In Screen.ActiveForm.Controls   ctlControl.FontName = strFont Next 

The Me Keyword

You'll find the Me keyword extremely useful because it refers to the object currently being executed. So, if you use Me in a form module, it refers to the form, and if you use it in a report module, it refers to the report. This allows you to write code that isn't dependent upon the form name, and that could be copied to another form module: another example of the benefit of loosely coupled code. For example, taking another look at the code to change the font of controls - it loops through the open forms. But what if we wanted to use this code in a single form - perhaps in response to a user request to change the font? One way would be to use the Forms collection, for example:

 For Each ctlControl In Forms("frmCompany").Controls 

One reason why this is bad is that you can't just copy the code - it would need changing if you pasted this into another form. Another reason is that it's wasteful - Access already knows what the active form is - Me :

 Dim frmCurrent As Form Dim ctlControl As Control For Each ctlControl In Me.Controls   ctlControl.FontName = strFont Next 

Now you might think that you could use ActiveForm , but Me and ActiveForm don't always refer to the same thing. For example, consider this diagram below:

click to expand

We have Form A , which is the active form (note: the highlighted toolbar). The button calls a public procedure in Form B , which displays the name of the form for Me and for the ActiveForm . Here's the result:

So this shows that even though DoSomething is executing in a different form, Me still points to the current form (that is the form under which the code is running), whereas ActiveForm is the form with the current focus.

team lib

Beginning Access 2002 VBA
Beginning Access 2002 VBA (Programmer to Programmer)
ISBN: 0764544020
EAN: 2147483647
Year: 2003
Pages: 256

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net