My experience with VBA in Access is 3 months long. I never used a Class
Module, though I got somewhat comfortable with Standard Modules. I heard
these are great tools to engineer an Application.
Can someone enlighten me about-
*why should I use them ?
I often use them to group together complex functions. For example, most of my apps make use of a 3rd party email
library, and I've built a class module which exposes the functions and such of that library to my app. The library can
connect to an SMTP server in several ways (authenticated, non-authenticated, etc) so to save myself the trouble of
determing which method to use, I built a class with a Connect method, and the class module's code in that method
examines the data present and makes an intelligent decision as to exactly how to connect. Now I can use this class
module without re-examining the somewhat arcane (to me, anyway) useage of the 3rd party library each time I import that
class into my new project; I just call the Class Modules Connect method and (hopefully) everything works as planned! I
do the same thing with Access User Level Security; I have a Class module that allows me to easily manipulate Users,
Groups, and Permissions without using the Access interface (so my enduser Admins can do this easily, without having to
learn anything about ULS).
If you deal with Digital Signatures for your codebase, scripts that could be used for malicious things (modifying the
registry, manipulating the File structure, etc) could be put into a class module for enhanced safety. See this link for
info, especially the "Things to do before signing a file or VBA Project" section:
http://office.microsoft.com/en-us/assistance/HP010397921033.aspx?mode=print
If you're using Access in bound mode, then you won't really use Class Modules for data or object handling, since Access
does this for you.
If you're using Access in un-bound mode, however, you can use class modules to handle your data needs. For example, you
could use a class module to validate data before it goes into the database.
You can also use Class modules to "encapsulate" complex logic, if needed. If, for example, you have an Ordering system,
you may need to build an order based on (a) the OrderHeaderDetails, (b) the OrderLineItems details, (c) Inventory
details (do I have these items in stock?), (d) Customer Details (who is this order going to?), etc etc ... using class
modules, I could build a class modules for Inventory, Customers, OrderHeaders, and OrderDetails ... I could then build
an Orders class module which interacted with all those different modules. Perhaps my main Orders class had a method
named "PostOrder", which could (a) mark the needed Inventory as "unavailable", (b) Fill in the OrderLineItems, calculate
the cost, and report that back to you (c) Fill in the OrderHeaderDetails (such as OrderDate, Shipping Method, etc) and
(c) add a new record to an Invoice table, perhaps, showing that a new Order has been placed and needs to be invoiced and
(d) automatically send an email to the customer with order details. Of course all this could be done with
code-behiind-forms, but class modules, to me, are a more elegant approach.
You can also pass Class modules around to Forms and Reports, if needed. For example, in one app I have a search utility
for WorkOrders that allows users to enter multiple criteria, return the results, and doubleclick on a Listbox to show
the underlying document. I simply build my clsWorkOrders class module on my search form, open my WorkOrders form, and
pass that class to a Property of my WorkOrders form. The property in the WorkOrders form accepts my class module and
fills the form appropriately.
*Where can I learn more about these ?
(The book I have on AccessVBA doesn't deal much with Class Modules except
a brief explanation.)
One of the best I've read on the subject is Rockford Lothka's "Visual Basic 6 Business Objects". He goes into great
detail about the subject. Check the online stores for pricing.
Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com