Class module

VBA class modules You can go a long way in Access database development without really using the object orientated features of Access VBA but a class module is a good way of improving the design of your database and making the code easier to maintain. On a small project, the class module gathers everything you need for a particular task into one self-contained unit so that it's easier to find. On a larger scale the self-contained class module can be reused if the same task comes up in different projects.

To be fair, this is something we've always been able to do with procedures and functions in a code module but the concept of object orientation makes it easier to develop, test and maintain something that does not depend on any external features.

Creating a class module

A class module is just VBA code and it appears in the same category as a regular code module on the screen. The only obvious difference is the icon - the prefix "cls" in the screenshot above is just my own naming preference.

Class module in the Access project screen A class module can be created from the main database window or from the VBA editor. It's easy in the editor, just select Class Module from the Insert menu. It's less obvious from the main window because there's no mention of class modules at all. The trick is to go to the Other area at the right-hand end of the Create menu bar. Click on Macro and a menu will appear with Class Module as an option. Click this and an empty edit window will open.

Oddly enough, once you've created a new class module the class icon will replace the macro icon on the ribbon.

The next page shows how to build a class that brings something of a dotNet flavour to calling the MessageBox function in Access.

More tips from Alvechurch Data

More tips from Alvechurch Data

Example of an Access class module

Using a VBA class module to handle messagebox dialogs

Read More

Object type codes in Access

Codes for the objects in the Access mSysObjects table.

Read More

DAO in MS Access

Using Data Access Objects (DAO) in Access VBA to populate a form.

Read More