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:
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
- Create the following form:
Form: frmMyform
-------------------------
Caption: fromMyform
ControlSource: <none>
Command button
-----------------------------------------
Name: Button0
Caption: My Button
OnClick: =ShowReport("Sales by Category")
- 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:
- Open the sample database Northwind.mdb.
- 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.
- Save the MySwitchboard form, and then open it in Form view.
- 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