Great Intro to Class Programming in VBA

Former Access MVP Ben Clothier's primer on class programming in VBA should be required reading for all Access developers.

Great Intro to Class Programming in VBA

For a great introduction to class programming in VBA, I can't think of a better starting place than this article from longtime Access MVP, Ben Clothier:

VBA Class Modules: Gateway to SOLID Code

Article Overview

Ben does a great job of explaining both why you should use class modules and how to do so with an excellent practical example.

Here's an outline of the article:

  • Introduction
  • Creating your custom types
  • Creating our first class module
  • Controlling access to methods via interfaces
  • Factory Design Pattern
  • Conclusion

I've written about some of these same topics, too.  If Ben's article piques your interest, check out the "Further reading" section below for additional information.

Two Approaches to the Factory Design Pattern

Ben uses a different factory design pattern than I do.  

His approach hides the initialization function in the main class module so that it can't be called accidentally from outside the object factory, but it does require additional class modules.  In and of itself, that's not a big deal, but the stock VBA development environment punishes you for having a lot of code modules.  

If you adopt Ben's approach, you should seriously consider using the Rubberduck VBA add-in, which allows you to group code modules into custom folders.


Further reading

Parameterized Constructors in VBA
VBA does not allow for parameterized constructors. We can work around that, though, using the Factory pattern and a strong naming convention.
Interfaces, Conceptually
Interfaces can be a difficult concept to grasp conceptually. To help make sense of the concept, consider these real world examples.
Interfaces, Practically
A real world example using Active Directory lookups.
Backward-Compatible Refactoring
How can you completely change the way you interact with a class without breaking backward compatibility? Interfaces.

Image by Gerd Altmann from Pixabay

All original code samples by Mike Wolfe are licensed under CC BY 4.0