Event-Driven Programming in VBA

Event-driven programming may sound complicated, but the concept is really quite simple.

Event-Driven Programming in VBA

You may have read or heard that VBA is an event-driven programming language or that Microsoft Access is an event-driven application.  That may sound complicated, but the concept is really quite simple.

A Layman's Definition

Let's break down this "event-driven" term as simply as possible.

Events

In the real world, "events" are nothing more than "things that happen."  So it is in the programming world.  

In Microsoft Access, most of those things that happen are actions that the user might take: clicking a button, opening a form, changing a report value, etc.  Almost every single time a user presses a key on the keyboard, moves the mouse, or clicks a mouse button some programming event happens.

Driven

"Driven" is simply the passive form of the verb "to drive."  This simply refers to the fact that events drive–or move–the application along.  

Contrast this with a typical command line application.  You call the application, maybe pass it a few parameters, and the whole thing runs start to finish on its own.

Access applications don't generally work this way; they are much more interactive.

Putting it together

Bringing the two terms back together, an "event-driven" application is one where the code sits around and waits for things to happen.  

A Real World Example

I find that real world examples often help me make sense of programming concepts.  Let's use an example from my childhood.

Funnel Ball

I admit I had no idea what this game was called.  It took me several minutes of Googling just to figure out what my header should be for this section.  I think I started with some idiotic search phrase like, "playground ball multiple choice."  By some miracle, that was enough to bring me an image of something similar to what I was actually looking for and, a few refined search terms later, an image of the exact toy I played with at the local playground growing up.

Anybody else out there remember funnel ball?

How to play funnel ball

The picture should be pretty self-explanatory.  You throw a ball into the funnel and then wait to see which hole the ball comes out.  The holes were numbered 2, 4, 6, and 8.

Funnel ball events

Let's list a couple of events related to funnel ball:

  • Ball Enters Funnel: this is what we'll call it when the ball enters the funnel
  • Ball Exits Funnel: this is what we'll call it when the ball leaves the funnel

Additional information

Sometimes it makes sense to include additional information about an event.  For example, when the ball exits the funnel, it must do so through one of the four numbered holes at the bottom of the funnel (unless you're playing with somebody like Jeremy Rolston who can find the loophole in any game and yes, Jeremy, we get it...if you throw the ball really high up in the air then when it hits the funnel it will bounce back out over the top and no, that does not mean you get infinity points and nobody can beat you because there's no number bigger than infinity when everyone knows that if those are the rules then whoever does it twice in a row gets double infinity and that person wins and you know what this is stupid I'm taking my ball and going home).

So, now when the Ball Exits Funnel, we want to know the Hole Number (2, 4, 6, or 8) of the hole from which it exits.

There's no extra information for the Ball Enters Funnel event because the ball can only enter the funnel from the top (shut up, Jeremy, you're ruining the game for everyone).

Recap

Event-driven programming sounds more computer science-y than it really is.  The actual concept is pretty straightforward.  In tomorrow's article, I will show you how we can represent the events above in VBA.

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