Microsoft KB Archive/324589

= How to use lightweight objects in Access 2002 =

Article ID: 324589

Article Last Modified on 8/2/2004

-

APPLIES TO


 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q324589



This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

Moderate: Requires basic macro, coding, and interoperability skills.



For a Microsoft Access 97 version of this article, see 159827.

For a Microsoft Access 2000 version of this article, see 208196.

IN THIS TASK
SUMMARY
 * Making an Object Lightweight
 * Method 1: Macro
 * Method 2: Public Function
 * Method 3: Hyperlink

REFERENCES



SUMMARY
This article first describes lightweight objects and shows you how to make an object lightweight. It then shows you how to create a lightweight switchboard form that uses the Hyperlink properties of form controls to open other objects in a database.

Lightweight objects first appeared in Microsoft Access 97. Lightweight forms and reports are objects that do not contain a class module. This makes lightweight objects smaller. Lightweight objects typically load and are displayed faster. They also make your database smaller. A disadvantage may be that a lightweight object does not appear in the Object Browser. Also, you cannot use the New keyword to create a new instance of a lightweight object.

By default, all new forms and reports are lightweight. Access creates a class module for the object only if you do one of the following:
 * Add Visual Basic code to an event property in the object.
 * Open the object in Design view, and then click Code on the View menu.
 * Set the HasModule property of the object to Yes.

back to the top

Making an Object Lightweight
You can convert an existing object that has a class module to a lightweight object by setting its HasModule property to No, and then saving the object.

WARNING: If an object has a class module, and you save the object with a HasModule property set to No, its class module and any code it contains are deleted.

If an object must perform certain actions that are assigned to events, you do not necessarily have to add modules to the object. Instead, you can use a macro or a public function.

back to the top

Method 1: Macro
If the code to perform certain actions is not complex, you can rewrite the code as a macro. Then, you can have the event call the macro instead of the code. This keeps the object lightweight.

For example, if a form has a Click event for a command button that previews a report that is named Sales by Category, you can create the following RunReport macro:   RunReport Action Arguments ---  OpenReport Report Name: Sales by Category View: Print Preview Then, you can set the OnClick property of the command button to the following:

RunReport

NOTE: If you want your macro to perform some actions and then to run Visual Basic for Applications code to do the rest, you can have the macro call a Visual Basic for Applications function with the command RunCode, as follows:   RunReport Action Arguments ---  OpenReport

Report Name: Sales by Category View: Print Preview

RunCode Function Name: MyFunction back to the top

Method 2: Public Function
You can also have the event in the object call a public function. If an object has an event that calls a public function, the object is still lightweight. By using the example in Method one, you can write a generic function that runs reports. You can then put that generic function in a public module. You can then call that function from the Click event. To see how this works, follow these steps:

CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

 Open the Northwind sample database. In the Database window, click Modules under Objects, and then click New.  In the new module, type or paste the following function: Function ShowReport(txtReport As String) DoCmd.OpenReport txtReport, acViewPreview End Function  Save the module, and then close the Visual Basic Editor. In the Database window, click Forms under Objects, and then click New</li>  Create the following form: <pre class="fixed_text">  Form: frmMyform -  Caption: fromMyform ControlSource:

Command button -  Name: Button0 Caption: My Button OnClick: =ShowReport(&quot;Sales by Category&quot;) </li> View the form in Form view, and then click the command button.</li></ol>

Note that this opens the preview of the Sales by Category report.

This method does not add a module to the form. Therefore, the form is still a lightweight form.

back to the top

Method 3: Hyperlink
By using the Hyperlink property of command button controls, label controls, and image controls, you can create a lightweight switchboard form in your database that does not use any Visual Basic code or macros.

The following example shows you how to create a switchboard form that uses hyperlinks to open database objects: <ol> Open the sample database Northwind.mdb.</li>  Create the following new form in Design view: <pre class="fixed_text">  Form: MySwitchboard ---  Caption: Main Menu

Command button ---  Name: OpenEmp Caption: Employees Form HyperlinkSubAddress: Form Employees

Label ---  Name: OpenCat Caption: Catalog Report HyperlinkSubAddress: Report Catalog

Image ---  Name: OpenSales Picture: C:\Windows\Circles.bmp PictureType: Embedded SizeMode: Clip PictureAlignment: Center PictureTiling: No  HyperlinkSubAddress: Query Category Sales for 1997 NOTE: If you do not have the file C:\Windows\Circles.bmp, you can substitute another bitmap or graphic file in the Picture property of the image control.

Look at the HasModule property of the form. Note that it is set to No. This is how you can tell that this is a lightweight form. </li> Save the MySwitchboard form, and then open it in Form view.</li> Click the Employees Form button, the Catalog Report label, and the image control, and note that each one opens the object that is specified in its HyperlinkSubAddress property.</li></ol>

back to the top

<div class="references_section">