Demystifying DoEvents

The DoEvents function may be the single most misunderstood piece of code in the entire VBA language. In this series of articles, I'll try to remedy that.

Demystifying DoEvents

If you've spent any time programming in Microsoft Access, you've likely run into a situation where the screen dimmed and the application title bar got updated to include the text "(Not Responding)."  After some Googling, you also probably found the simple solution: add a call to DoEvents.

The DoEvents function was largely a mystery to me for a long time.  I used it much like a cargo cult programmer would.  Whenever the screen dimmed and I got the dreaded "(Not Responding)" text in my application title bar, I would start throwing DoEvents calls into my code all willy-nilly.  

Still "(Not Responding)"?  Let's throw some more DoEvents calls into the mix.

I was like Oprah celebrating an episode milestone, but instead of guests I was shouting to my procedures,  "You get a DoEvents call.  And you get a DoEvents call.  And you get a DoEvents call!"

It's not programming. It's just magic! And I don't know what I'm doing any more!!!

Needless to say, typing random code into your editor in the hopes of appeasing the Microsoft gods is no way to develop software.

In this series of articles, I'll dive deep into the DoEvents function.  I'll demystify this function so that you can start using it with intention, instead of haphazardly the way I used to still do sometimes.

I'm going to explore the following topics:

UPDATE: Bonus Articles!

And hopefully, if all goes well, you will finally be able to get rid of those wooden headphones you've been keeping around "just in case."

Base Image by Susie Bailey from Pixabay (wooden ball Image by GGs_ from Pixabay) – cartoonishly bad combination of the two images by Mike Wolfe from NoLongerSet

External references

Access VBA Loop (Not Responding)
I am looping through a recordset to carry out some basic functions or edits. Usually with recordsets with more than 50 records, access will stop responding. I have me.repaint before the loop com...
DoEvents Function
The function, method, object, or property described in this topic is disabled if the Microsoft Jet Expression Service is running in sandbox mode, which prevents the evaluation of potentially unsafe expressions.
Cargo cult programming - Wikipedia
Cargo Cult Software Engineering | Steve McConnell
Issue: March/April 2000 | PDF

Article references

How to Teach a Six-Year Old About the DoEvents Function
I challenge you to read this article without laughing or learning anything new about the DoEvents function.
How to Use the DoEvents Function: A Demonstration
The demonstration and downloadable sample code in this article make it easy to understand how DoEvents works.
When (or Should I Say, How Often) to Call DoEvents
Too few calls to DoEvents, and your app may appear to be ”(Not Responding).” Too many calls to DoEvents, and your app will run slow. Let’s Goldilocks this thing.
A High-Level Overview of the DoEvents Function Call
Why does calling DoEvents save us from the dreaded ”(Not Responding)” treatment? It’s all about handling “Windows Messages.”
How DoEvents Works: The Nitty-Gritty Technical Details
A peek behind the scenes at the inner workings of the DoEvents function.

Image by PublicDomainPictures from Pixabay

UPDATE [2021-07-21]: Added link to bonus article, DoEvents vs. Repaint

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