Microsoft Access VBA Macro Programming

(Tina Sui) #1

Chapter 21 Class Modules.


Chapter 21 Class Modules.


A


s you program in VBA and insert modules to hold your code, you will notice from
the menu that you can also insert what are called class modules. These are
different from ordinary modules in that they allow you to create a component
object model (COM) of your own.
Class modules cannot be run in the same way as a standard module procedure and must be
referenced from your code within a module. This allows you to create your own objects and
collections such as theTableDefor theTableDefscollection. Unfortunately, you cannot
create a DLL (dynamic link library) file, which is what you would do if you were programming
in full Visual Basic or C++/C#. However, a class module is still part of a database and you
can put a reference into your class module database from another database in order to use it.
This makes it a component object, and it effectively adds multitier architecture to your
applications.
Earlier in this book, you learned that Access is a multitier application because there is a
client services layer that has the Access object model and other object models sitting beneath
it, and the data services layer sits under that. The class modules allow you to place another
layer between the client services layer and the Access object model or between the client
services layer and an external data source, such as Oracle or SQL Server.
Your class module database can then be used as a reference to your object in another
database. However, other programmers who use it won’t see the underlying code and rules
you have built into it if you have used a password. As soon as you put a reference to your
class module database, the public functions and subroutines can be accessed from other
modules within a database, although if you have password-protected it, other programmers
will not see the underlying code.
As an example, aTableDefis an object. This contains the hierarchy of objects for each
table within your database. One of the collections within this object is Fields, which defines
all the fields for that table. You can find out the number of fields in a table by using theCount


265

Free download pdf