Access 2007-2019
Access 2000-2003
Access Seminars
TechHelp Support
Tips & Tricks
Access Forum
Course Index
Topic Glossary
Insider Circle
Home   News   Courses   Seminars   Templates   Help   TechHelp   Forums   Order   Contact   Logon  
Access Tip Sleep Timer
By Richard Rost   Richard Rost on Facebook Richard Rost on Twitter Richard Rost on LinkedIn Email Richard Rost   8 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.


Post a New Comment or Question
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:

1/19/2021Reminder Popup
1/18/2021Separate Username in Email
1/16/2021Access Developer 24
1/16/2021Access Developer 24 Lessons
1/16/2021Access Developer 24
1/16/2021Access Developer 23
1/16/2021Last Name, First
1/16/2021TechHelp Free Templates
1/15/2021Access Gantt Chart Planner

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

YouTube Channel    LinkedIn

Blog RSS Feed   
Keywords: access tips sleepsec sleep timer delay  PermaLink