Database development and training for Birmingham and the midlands
Specialists in Microsoft Access and Visual Foxpro database training and development
I am happy to hear from existing clients but I am taking no new calls.
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.
A class module can be created from the main database window or from the VBA editor. It's easy in the editor, just selectfrom the 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 area at the right-hand end of the menu bar. Click on and a menu will appear with 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.