Computer Learning Zone CLZ Access Excel Word Windows

Curious that we spend more time congratulating people who have succeeded than encouraging people who have not.

-Neil deGrasse Tyson
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Home > Code Vault > Generic VBA > SleepSec
Sleep Seconds
By Richard Rost   Richard Rost on LinkedIn Email Richard Rost   6 years ago

This procedure will determine how long Alex naps for.

Code

Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)

Public Sub SleepSec(NumSec As Long, Optional AllowEvents As Boolean = True)
    Dim X As Integer
    For X = 1 To NumSec
        Sleep 1000
        If AllowEvents Then DoEvents
    Next
End Sub

Here's a version with a visible countdown timer (requires StatusBox)

Public Sub SleepSec(NumSec As Long, Optional AllowEvents As Boolean = True, _
                    Optional ShowCountdown As Boolean = True)

    ' sleep for a number of seconds, whole seconds only, sleeps 1 second at a time
    ' use for delays of 1 second or longer
    ' added a visible countdown using my Status function https://599cd.com/Status
    
    Dim X As Long
    For X = NumSec To 1 Step -1
        If ShowCountdown Then Status "Wait " & X & "..."
        Sleep 1000 ' sleep 1 full second
        If AllowEvents Then DoEvents
    Next
    
End Sub

Member Version

There is a more elaborate Members' Only version that's covered in the Extended Cut of the Sleep Countdown TechHelp video. You can find the code here.

Usage

Put the above in a global module, then anywhere in your database you can say something like this to sleep for 5 seconds:

SleepSec(5)

The purpose of the loop is to make SleepSec responsive. If you sleep for 10 seconds without any DoEvents, then your code might appear to freeze. If this interferes with performance or function for you, just set it to FALSE.

Video

This code is explained in my Sleep TechHelp video.

 

Comments for Sleep Seconds
 
Age Subject From
2 monthsStatusbox UpgradeRichard Rost
2 yearsSleepBrad Wile

 

Start a NEW Conversation
 
Only students may post on this page. Click here for more information on how you can set up an account. If you are a student, please Log On first. Non-students may only post in the Visitor Forum.
 
Subscribe
Subscribe to Sleep Seconds
Get notifications when this page is updated
 
 
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 3/8/2026 7:45:34 PM. PLT: 0s
Keywords: code vault generic vba sleepsec sleep seconds  PermaLink  Sleep Seconds