HandsOn Working with Object Variables

1. Load another instance of Microsoft Office Access 2003 and open the Acc2003_Chap01.mdb file that you worked with in Chapter 1. Both instances need to be open for this example.

2. Press Alt+F11 to open Visual Basic Editor window.

3. Choose Insert | Module to add a new module.

4. Enter the code of the HideControl procedure shown below in the new module's Code window.

Part I

5. To run the procedure, click any line between the Sub and End Sub keywords and press F5 or choose Run | Run Sub/UserForm.

Sub HideControl()

' this procedure is run against the open Customers form ' in the Northwind database Dim frm As Form Dim myControl As Control Set frm = Forms!Customers Set myControl = frm.CompanyName myControl.Visible = False End Sub

The procedure begins with the declaration of two object variables called frm and myControl. The object variable frm is set to reference the Customers forms. For the procedure to work, the referenced form must be open. Next, the myControl object variable is set to point to the CompanyName control located on the Customers form. The statement

Set myControl = frm.CompanyName is the same as

Set myControl = Forms!Customers.CompanyName

Instead of using the object's entire address, you can use the shortcut — the name of the object variable. The purpose of this procedure is to hide the control referenced by the object variable myControl. After running the HideControl procedure, switch to the Microsoft Access window containing the open Customers form. The CompanyName control should not appear on the form. To make the control reappear, modify the last line of this procedure by setting the Visible property of myControl to True and rerun the procedure.

0 0

Post a comment