Access 2007-2019
Access 2000-2003
Access Seminars
TechHelp Support
Tips & Tricks
Access Forum
Course Index
Topic Glossary
Insider Circle
Home   Courses   Seminars   Templates   Help   TechHelp   Forums   Contact   Join   Order   Logon  
Access Tip Sleep Timer
By Richard Rost   Richard Rost on Twitter Richard Rost on LinkedIn Email Richard Rost   14 months ago

Want to add a delay in your Access VBA code? This is sometimes handy if you want to give the user time to react, or slow down processing so you can see what's going on, or even wait for a background process to finish. 

First, declare the following at the top of your module:

#If VBA7 Then
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

This is a Windows API function that you can use to pause a particular number of milliseconds. So:

Sleep 1000

would have processing sit there for a full second not doing anything. This function by itself locks up the whole computer and doesn't allow any input for that second, which is fine if it's only a second, but if you want, say, a 15 second pause, you're locking up the computer for a whole 15 seconds. So I created this function to go with it:

Public Sub mySleep(Optional Seconds As Double = 1, Optional AllowDoEvents As Boolean = True)
    ' this function will break up seconds into quarters of a second
    Dim SecFrac As Double
    SecFrac = 0
    While SecFrac < Seconds
        Sleep 250
        If AllowDoEvents Then DoEvents
        SecFrac = SecFrac + 0.25
End Sub

What this does is break up the timer into quarter second chunks (250 milliseconds) and the DoEvents code allows processing to continue if something else is happening in the background. It also allows you to put an ABORT button or checkbox on your form to give the user a chance to cancel it.

Now you can say:

While Not Abort
    ' Do some stuff here
    mySleep 5 ' sleep for 5 seconds

If anyone would like to see me make a video lesson about this, let me know.


Delay allows maintaining focus Upload Images   Link 
John Hirschy 
6 days ago
I could not get the first item to work. Placing the material at the beginning of the module prevented the module from working.  Something is obviously being left out that Access needs to function.  Perhaps a video would help. Read More...
Richard Rost
6 days ago
Watch my latest TechHelp video about 64-Bit. I cover the Sleep function in that. Did you place the code in a Global Module? Can't be in a Form Module.
Add a Reply

Start a NEW Conversation
Only students may post right now. Click here for more information on how you can set up an account. If you are a student, please LOG ON first.

You may want to read these articles from the 599CD News:

7/28/2021Membership Database
7/27/2021Quick Queries #5
7/27/2021Loop Thru Fields in Table
7/26/2021First Monday
7/25/2021Missing Months
7/24/2021Center Vertically
7/22/2021Buy Access
7/21/202164-Bit Access

Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Account Login
Online Theater
Lost Password
Student Databases
Change Email
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Customer Support
Web Site Tour
Consulting Services
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Video Tutorials
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Terms of Sale
Contact Info
Support Policy
Email Richard
Mailing Address
Phone Number
Fax Number
Course Survey
Blog RSS Feed    Twitter

YouTube Channel    LinkedIn
Keywords: access tips sleepsec sleep timer delay  PermaLink