Sleep Countdown
By Richard Rost
21 days ago
Add a Visible Sleep Countdown Timer Delay in Access
In this video, we will talk about how to add a visible sleep countdown to your Microsoft Access database using VBA. Instead of letting your application appear frozen during pauses, I will show you how to upgrade the SleepSec function to display a live countdown timer using the status box, keeping your users informed that the program is still running. We will walk through updating the function with an optional countdown, discuss the benefits of allowing events during the wait, and demonstrate how to integrate this into your own Access projects.
Members
In the extended cut, we will change the countdown display so the message replaces itself in place, instead of showing multiple lines for each number. I will show you how to update the status box message to refresh in the same location, giving a cleaner look similar to how old DOS applications used to overwrite text on the same line.
Silver Members and up get access to view Extended Cut videos, when available. Gold Members can download the files from class plus get access to the Code Vault. If you're not a member, Join Today!
Prerequisites
Links
Recommended Courses
Keywords
TechHelp Access, sleep countdown, VBA sleep function, sleepsec, status box, countdown display, optional Boolean parameter, show countdown, DoEvents, hard pause, user feedback, global module, class module, drag and drop file handling, pop-up message, code vault
Intro In this video, we will talk about how to add a visible sleep countdown to your Microsoft Access database using VBA. Instead of letting your application appear frozen during pauses, I will show you how to upgrade the SleepSec function to display a live countdown timer using the status box, keeping your users informed that the program is still running. We will walk through updating the function with an optional countdown, discuss the benefits of allowing events during the wait, and demonstrate how to integrate this into your own Access projects.Transcript Your Access database does not have to look frozen while it is waiting. You can show the user a live countdown timer right on the screen.
Welcome to another TechHelp video brought to you by AccessLearningZone.com. I am your instructor, Richard Rost. Today, we are going to talk about how to add a visible sleep countdown to your Microsoft Access database using a simple VBA function.
In other words, instead of your code just pausing for five seconds with no feedback, you can display five, four, three, two, one, lift off, so the user knows something is happening and that the program is still alive and they did not lock it up.
We will take the basic SleepSec function that we built in my previous sleep video and update it with an optional countdown display using the status box. I will show you how to make those waits feel clean and professional.
A little while back, I did a video on the sleep function, which you can use to tell the computer, wait, wait, wait, hold on. Something is going to happen. You can use it for send keys. You can use it all kinds of places. It is a versatile function and I use it a lot to get the database to pause and wait for something else to happen.
In that previous video, I showed you how to write a subroutine called SleepSec where you can give it a number of seconds and it will wait that number of seconds. You could say SleepSec 5, and it will sleep five seconds, but importantly, the whole system will not appear to be locked because this will do a one second loop. In each second, it will do events that will allow you to update things on the screen if you have loops running, that kind of stuff. This is a more versatile function than just using sleep by itself.
This is available in my code vault. I will put a link down below. You can grab a copy of it here.
Today, what I am going to show you is I made a little upgrade to it. Instead of just sitting there for five seconds, ten seconds, whatever it is, without seeing anything, we are going to have it give you a little countdown. On the screen, it will say wait five, four, three, two, one, so the user knows something is actually going on. Real simple today.
First, go watch this video if you have not already. It is a developer level video so you need a little bit of VBA. This video explains what you need to know to move forward.
To be clear, we did use a sleep countdown in this video, but we did it outside of the SleepSec function. Today, we are going to make this a reusable function so you do not have to worry about doing the countdown manually yourself. The SleepSec function will take care of it for you.
We are going to use my status box function to display stuff on the screen. If you have not seen this in one of my previous videos, go watch this video too. It is basically a replacement for message box, so you can just display messages on the screen for the user in a little text box.
Here I am in a TechHelp free template. This is a free database. You can grab a copy off my website if you want to. Down here in the global module, you will find - let me resize this. There it is. There is my status function and my SleepSec function.
What we are going to do is we are going to give the user the option to show that countdown. They can or they cannot, it does not matter. It is up to them. We are going to add another optional on here. I am going to go to the next line so it is not running all off the line there. We are going to say Optional ShowCountDown As Boolean = True. We will make the default True so all existing ones that you have will just start showing the countdown. If the user wants to turn it off, they can set that default.
I am going to switch this to Long. Why? When I initially wrote it, I used Integers a lot. This function is actually quite old. Now I use Long integers for everything because why not? Memory is cheap. We do not need to worry about it.
Now we are going to reverse this. Instead of going from 1 to X, we are going to go from X to 1 backwards. That says 5, 4, 3, 2, 1 is a countdown. We are just going to change this. For X = NumSec, the number of seconds, to 1, Step -1. That is how you go backwards in a loop. Go 5, 4, 3, 2, 1.
Now right here is where we are going to say if ShowCountDown, remember you do not have to write = True - that is assumed - then we are just going to status the message status. What do you want it to say? Wait, and then X, and then dot, dot, dot. So we will say wait, 5. Then wait, 4. Then it will do its wait. It will do its little sleepy dance here.
This is an older version. I've got in here If AllowEvents, then DoEvents. Sometimes you do not want DoEvents to run. You might want a hard sleep where you do not allow events to run. You might want like a 5 second pause. If DoEvents is allowed to run, other events can run, like the user can click on other stuff, click on other buttons, form open events might run. Other timed events might go off. If you do not allow events, it is a hard wait. You might want it to not allow anything to happen for 10 seconds. If that is the case, then turn DoEvents off and do not allow events. Not sure why that was not in the code in here, but I do not know. That is in my code vault.
All right, let's give her a test. Debug compile once in a while. I am going to close it. Close it. Always close your forms and restart them.
Ready? Click. And this moves. Slide that up. Then we will hit go. Oh, I did not tell it to sleep, did I? Let's put that in there now too.
Design view, let's go into the button right here. We will do in here status "testing countdown." And we will do SleepSec 5. Then "hello world."
Save it. Then we will do another debug compile, close it, close it, open it. Ready? Here we go. Click. Three, two, one. Lift off.
There you go. Now the user is not going to sit there wondering why the system seems like it is frozen. It is just little things like this that make your database and your user experience a little bit better. I am all about that.
I posted about this this morning when I just made a little update myself. I was not going to initially do a video about this because it is a simple little upgrade, but Kevin talked me into it. So I am like, oh, yeah, that could be a nice little video. I like it. I hope you guys approve.
And, hey, quick, while I got you, hit that like and subscribe for me. It helps me out.
Now members, in the extended cut, we are going to do it without having to see five lines for five, four, three, two, one. We are going to replace the previous one, so it changes right in place.
Remember that trick with old DOS applications way back in the day? It would write out a line, but it would not hit the new line character so it could just backspace over it and write it again with a different one. It is a similar trick to that. That will be in the extended cut for the members.
Remember, as an UP, you get access to all of my extended cut videos, and Gold members get access to my code vault. This particular one, SleepSeconds, is free for everybody. I left this one open, but there is tons and tons of all kinds of VBA in here. There is generic VBA, there is Access specific VBA, there is tons and tons and tons of stuff in here that I have been adding for years. So yeah, there is lots of stuff in here.
If you like learning this stuff, check out my Access Developer Lessons on my website. I have lots of them. I just released Access Developer Level 51. Yeah, I have 51 of these things already. We do a couple of cool things - we do a pop-up that pops up over wherever you click. If you have a big long form, if you click over here, the pop-up pops up over here. If it is down here, it will do it over here. We make it a class module, which is really cool. We start a drag and drop file handling system so you can take pictures and you can just drag them and drop them on a form, and they get saved in the database folder. It is really cool. Check this out.
That is going to do it, folks. If you are going to make Access wait, do not leave your user staring at the screen, wondering if the database just crashed. By wrapping the sleep function in my SleepSec routine, you can pause in one second intervals and keep the interface responsive with DoEvents when you want it and, most importantly, show a visible countdown message so the user knows exactly what is going on. Small upgrade, huge difference in overall user experience.
If you found this helpful, post a comment down below. Let me know how you like today's video and how you plan to use the sleep countdown in your own database.
That is going to do it for your TechHelp video for today, brought to you by AccessLearningZone.com. I hope you learned something. Live long and prosper, my friends. I will see you next time, and members, I will see you in the extended cut.Quiz Q1. What is the main improvement discussed in this video for the sleepsec VBA function in Microsoft Access? A. It automatically closes forms after a pause B. It displays a visible countdown during the wait time C. It sends an email when the wait finishes D. It can only pause for integer seconds
Q2. What is the primary benefit of displaying a countdown during a pause in your Access database? A. It hides other ongoing events in the database B. It tells the user the database is still working and not frozen C. It prevents the user from closing the form D. It exports data during the countdown
Q3. What does the optional Boolean parameter showcountdown do in the updated sleepsec function? A. Determines the length of the pause B. Decides whether to run the code in the background C. Controls whether the countdown is shown on screen D. Sets the color of the status message
Q4. Why are long integers preferred over regular integers in the updated code? A. They use less memory B. They allow negative numbers C. They are easier to type D. Memory is plentiful and they can store larger values
Q5. What built-in VBA method is used to keep the user interface responsive during the sleepsec countdown? A. Wait B. Pause C. DoEvents D. Sleep
Q6. What is the effect of disabling DoEvents during the sleepsec routine? A. Other events and interface updates can still run B. The pause becomes a hard wait and nothing else happens C. VBA code execution speeds up D. The countdown disappears
Q7. What is the default value set for showcountdown in the sleepsec function? A. False B. 0 C. True D. 1
Q8. In the countdown loop, what is the proper VBA syntax for making it count backwards from x to 1? A. For x = 1 To numsec Step -1 B. For x = numsec To 1 Step -1 C. For x = numsec DownTo 1 D. While x > 0
Q9. What does the status function do in the context of this video? A. Delays code execution by one second B. Shows a message in a pop-up window C. Displays messages for the user in a text box on screen D. Logs errors to a file
Q10. What is the main purpose of wrapping the sleep function in the sleepsec routine with one-second intervals? A. To keep the system busy and use up CPU B. To lock the user interface and prevent changes C. To keep the interface responsive and allow countdown updates D. To secure the database from outside access
Answers: 1-B; 2-B; 3-C; 4-D; 5-C; 6-B; 7-C; 8-B; 9-C; 10-C
DISCLAIMER: Quiz questions are AI generated. If you find any that are wrong, don't make sense, or aren't related to the video topic at hand, then please post a comment and let me know. Thanks.Summary Today's video from Access Learning Zone covers how to add a visible sleep countdown to your Microsoft Access database using a simple VBA function. This allows you to communicate to your users that the program is processing, rather than just freezing for a few seconds with no feedback. Instead of the code silently waiting, you can show a countdown — five, four, three, two, one — to let the user see progress and confirm that the application is still responsive.
We start by updating the basic sleepsec function that I demonstrated in a previous tutorial. That original function let you tell Access to pause for a specified number of seconds, such as sleepsec 5, and would sleep for five seconds. The benefit of this approach, as compared to a simple sleep command, is that it does not lock up the whole interface because it works in a loop, waiting for one second intervals and running DoEvents, allowing the application to remain responsive and update on-screen elements if necessary.
That code is available in my code vault, and you will find a link below if you want to grab a copy. Today, I am showing you how to upgrade that function so it no longer just sits idle during the wait. With a built-in visible countdown, you and your users can see a message counting down the seconds, helping reassure everyone that the program is active.
Before continuing, I suggest you watch my earlier video that introduces the sleepsec routine. Some general VBA knowledge is needed to follow along. The key difference today is that, instead of managing a countdown message externally, I have now incorporated it directly into the sleepsec function, so you do not need to handle the messages manually.
To display messages on screen, I am using my status box function. If that is new to you, I also have a dedicated video about how the status box works. Essentially, the status box gives you a more flexible way to display live messages to your users than the standard message box.
Inside my free TechHelp template database, you can find the status and sleepsec functions in the global module. You can download this database from my website if you like, and see the working code yourself.
For this update, I am giving users the option to show the countdown or not. There is a new optional parameter in the sleepsec function called showcountdown, which defaults to true. That means any existing calls to sleepsec will now show the countdown unless you choose to turn it off. I also updated the data type used in the function to long. Although the original function used integers, I now prefer using long integers, as there is little reason not to do so with modern computers.
To implement the countdown, I reversed the direction of the loop — going from the number of seconds down to one, rather than counting up. This effectively creates the countdown effect, displaying numbers in descending sequence.
The code is written so that, if showcountdown is enabled, the status box will display a message such as "Wait... 5" and then count down each second. Between each display, the function sleeps for one second.
There is also a section in the code dealing with DoEvents. Sometimes, you might not want application-level events to occur during the wait — for example, if you want a completely unresponsive pause, preventing any user actions or other events from running. If you want this kind of "hard" pause, you can turn off events so nothing happens for the duration of the sleep. This level of control is particularly useful if you want to avoid unwanted actions by users or other automation during the wait.
To test the function, I went into the form code, added a call to sleepsec with five seconds, along with a sample status message and a follow-up message to confirm completion. After compiling and restarting the form, pressing the button shows the countdown in action, counting down from five to one.
With this countdown, users are no longer left wondering if the system has frozen. In my experience, adding small usability enhancements like this can make a huge difference in how users perceive your database applications.
When I added this update, I was not planning to make a new video since it seemed like a minor improvement, but several people encouraged me to share it. It's a small and simple change, but it makes your projects feel much more user friendly.
For members, in the extended cut of today's video, I will demonstrate how to display the countdown without printing multiple lines, but instead updating a single line in place — similar to older DOS applications that would overwrite the same display area. This is a neat trick that can make the countdown even cleaner.
Members get access to all of the extended cut videos, and gold members can access the entire code vault. The sleepseconds routine is free for everyone, but there is a lot of additional Access and generic VBA code available for members.
If you are interested in learning more, I encourage you to check out my Access Developer lessons, which cover a variety of advanced topics. In my latest level, we create a pop-up that appears right where you click on a form and use a class module for more modular code. We also start working on a drag-and-drop file handling system, so you can drop files or pictures straight onto your forms and have them saved directly to your database folder.
To wrap up, if you need to make your Access application wait, do not leave your users staring at a frozen screen. By using my updated sleepsec routine, you can pause execution in one second intervals, keep the interface responsive, and most importantly, show users exactly what is happening. A small improvement like this can have a significant impact on the overall quality of your applications.
You can find a complete video tutorial with step-by-step instructions on everything discussed here on my website at the link below. Live long and prosper, my friends.Topic List Adding a sleep countdown to Microsoft Access using VBA
Modifying the sleepsec function to include a countdown
Using the status function to display countdown messages
Implementing an optional show countdown parameter
Reversing a VBA For loop to count down seconds
Displaying a visible countdown with Wait X... messages
Explaining use of Boolean option for countdown display
Switching from Integer to Long data type in VBA loops
Discussing AllowEvents for DoEvents control during sleep
Testing the countdown in a sample Access form
Integrating sleepsec countdown into button click events
Demonstrating improved user feedback during waitsArticle If you have ever needed to add a pause or delay in your Microsoft Access database using VBA, you may already know about the sleep function or its variants. A simple pause is often useful for letting things process, pausing between send keys, or sequencing actions where timing matters. However, a common user-experience problem with simple pauses is that the screen appears frozen. The user cannot tell whether the program is working or has locked up. To address this, I will show you how to upgrade a standard waiting routine so that it displays a visible countdown, letting your users know exactly how long remains in the wait.
Let us start by considering the basic approach. A traditional sleep function simply halts code for a set number of milliseconds or seconds. The problem is that during this time, the interface does not update and the user cannot interact with the database. Using DoEvents inside a custom waiting function solves part of this by letting Access process screen updates and other pending events between increments of your wait.
Suppose you already use or have written a function called SleepSec in VBA. This subroutine pauses execution for a given number of seconds, but because it loops once per second and includes DoEvents, the interface stays responsive. That means that during the wait, Access can process redraws and handle certain background tasks. It is generally more user-friendly than a single, hard pause.
We will now enhance SleepSec by displaying a countdown message using a Status function. This Status function is simply a routine that updates a message somewhere on your user interface—perhaps in a small textbox or label on your form, or in a custom status box. It works somewhat like the classic MsgBox, but instead of interrupting the flow, it just updates text somewhere on the screen, so the user gets feedback without having to click anything.
Let us build the upgraded SleepSec routine. The goal is to have it display a countdown in seconds, such as "Wait... 5", "Wait... 4", and so on, counting down each second until the pause is finished. This gives the user reassurance that the system is working.
Here is what the improved SleepSec routine looks like in VBA:
Sub SleepSec(NumSec As Long, Optional ShowCountdown As Boolean = True, Optional AllowEvents As Boolean = True) Dim x As Long For x = NumSec To 1 Step -1 If ShowCountdown Then Status "Wait... " & x & "..." End If If AllowEvents Then DoEvents End If Call Sleep(1000) Next x Status "" ' Clear the status box, or update as needed End Sub
In this version, NumSec is the number of seconds to wait. ShowCountdown is an optional Boolean parameter (default True) that lets you choose whether to show the countdown or not. AllowEvents (also optional and defaulting to True) determines if Access should call DoEvents during the wait, which keeps the interface responsive but can allow other code or timers to run.
The loop starts at NumSec and counts down to 1. Each second, if ShowCountdown is true, it calls Status with the countdown message—such as "Wait... 5...". After optionally calling DoEvents, it then calls the standard Sleep function (you will need to declare or import Sleep from the Windows API if it is not already in your globals). After the countdown finishes, you may want to clear the status message by calling Status with a blank string.
To use this in practice, suppose you have a button on a form that triggers a process which needs a pause. In the click handler, you might write something like:
Status "Testing countdown..." SleepSec 5 Status "Hello world!"
When the user clicks the button, they will see "Testing countdown...", then a countdown from five to one, then finally "Hello world!". The pause is visible and interactive, the interface does not appear locked, and users are not left wondering if the database has crashed.
A quick note about the DoEvents parameter: if you want to ensure nothing else can happen during your pause—maybe you do not want the user to be able to click other buttons or trigger other events—you can set AllowEvents to False. This gives you a "hard" wait where only your countdown updates, and all other Access events are suppressed for the duration.
Overall, by wrapping the standard wait logic inside this more flexible SleepSec routine and including a visible countdown, you make your Access applications feel much more responsive and user-friendly. Small touches like this make a big difference in professionalism and user experience. Feel free to expand on this by customizing your Status display or integrating it into more complex workflows. If you manage VBA libraries or share code modules with others, including optional parameters and thoughtful UI updates like this can really set your code apart.
|