Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: Using Lightweight Objects in Access


View products that this article applies to.

This article was previously published under Q208196
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 2002 version of this article, see 324589 (http://support.microsoft.com/kb/324589/EN-US/ ) .

↑ Back to the top


Summary

This article describes the concept of lightweight objects and provides a technique to create a lightweight switchboard form that uses the Hyperlink properties of form controls to open other objects in the database.

NOTE: This article explains a technique demonstrated in the sample file, FrmSmp00.mdb. For information about how to obtain this sample file, please see the following article in the Microsoft Knowledge Base:
233324� Microsoft Access 2000 Sample Forms Database Available in Download Center

↑ Back to the top


More information

Lightweight forms and reports, which are objects that do not contain a class module, were first introduced in Microsoft Access 97. One advantage of lightweight objects is that they are smaller and typically load and are displayed faster than objects with class modules. Also, because they require no storage space for a class module, they can decrease the size of your database. Possible disadvantages of using lightweight objects is that they do not appear in the Object Browser, and you cannot use the New keyword to create a new instance of the object.

When you create a new form or report, it is a lightweight object by default. Access creates a class module for the object only if you do any 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.
You can convert an object with a class module into 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.

Making an object Lightweight

If you need an object to perform certain actions assigned to events, there are techniques to doing this without adding any modules to the form.

Method 1

If the code is not very complex, you can re-write it as a macro and have the event call the macro instead of the code. If you call a macro, the form remains lightweight.

For example, if on a form you have a Click event for a command button that previews a report called Sales by Category, you could create the following macro called RunReport
   RunReport Action Arguments
   -------------------------------
   OpenReport
   Report Name: Sales by Category
   View: Print Preview
				
and then 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 in the following example:
   RunReport Action Arguments
   -------------------------------
   OpenReport
   Report Name: Sales by Category
   View: Print Preview

   RunCode
   Function Name: MyFunction()
				

Method 2

You can also have the event assigned to the object call a public function. An object is still lightweight if it has an event that calls a public function. Using the example in Method one, you could write a generic function that runs reports and place that 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 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 click the command button.
Note that you receive the preview of the Sales by Category report.

By using this method, the form has no module behind it, and therefore it is a lightweight form. Yet, you have the ability to call Visual Basic for Applications code. And you can still specify which report you want to preview in the Click event.

Method 3

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 creates a switchboard form that demonstrates the use of hyperlinks to open database objects:
  1. Open the sample database Northwind.mdb.
  2. Create the following new form not based on any table or query 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 1995
    						
    NOTE: If you do not have the file C:\Windows\Circles.bmp, you can substitute another bitmap or graphic file on your computer in the Picture property of the image control.

    Note that the HasModule property of the form is set to No. That 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 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


Keywords: KB208196, kbdta, kbinfo

↑ Back to the top

Article Info
Article ID : 208196
Revision : 1
Created on : 12/12/2002
Published on : 12/12/2002
Exists online : False
Views : 316