How to use lightweight objects in Access 2002 (324589)



The information in this article applies to:

  • Microsoft Access 2002

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

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.

  1. Open the Northwind sample database.
  2. In the Database window, click Modules under Objects, and then click New.
  3. In the new module, type or paste the following function:
    Function ShowReport(txtReport As String)
       DoCmd.OpenReport txtReport, acViewPreview
    End Function
    					
  4. Save the module, and then close the Visual Basic Editor.
  5. In the Database window, click Forms under Objects, and then click New
  6. Create the following form:
       Form: frmMyform
       -------------------------
       Caption: fromMyform
       ControlSource: <none>
    
       Command button
       -----------------------------------------
       Name: Button0
       Caption: My Button
       OnClick: =ShowReport("Sales by Category")
    					
  7. View the form in Form view, and then click the command button.
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:
  1. Open the sample database Northwind.mdb.
  2. Create the following new form in Design view:
       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.
  3. Save the MySwitchboard form, and then open it in Form view.
  4. 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.
back to the top

REFERENCES

For more information about the HasModule property, click Microsoft Access Help on the Help menu, type HasModule property in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

For more information about class modules, click Microsoft Access Help on the Help menu, type class modules in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

For more information about the HyperlinkSubAddress property, click Microsoft Access Help on the Help menu, type HyperlinkSubAddress property in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

back to the top

Modification Type:MajorLast Reviewed:8/2/2004
Keywords:kbProgramming kbhowto kbdta KB324589