Access 2007-2019
Access 2000-2003
Access Seminars
TechHelp Support
Tips & Tricks
Access Forum
Course Index
Topic Glossary
Insider Circle
 
Home   News   Courses   Tips   Templates   Forums   Help   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   5/30/2020 6:24:39 AM

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:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

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
    Wend
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
Wend

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.
 
Subscribe
 

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

8/11/2020Access Maximize App Window
8/10/2020Access Beginner Courses
8/10/2020Access Beginner Level 1
8/10/2020Access 102
8/10/2020Access 101
8/10/2020Access Subform in a List Form
8/9/2020Access Odd or Even?
8/9/2020Access AutoNumbers Good or Bad?
8/6/2020Access Sync Up Remote Database
8/6/2020Access Customer Codes
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Account Login
Online Theater
Downloads
Lost Password
Free Upgrades
Insider Circle
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Articles
Search The Site
Waiting List
Production Schedule
Collapse Menus
Help
 
Live Chat
Customer Support
WalkThru Tutorials
Troubleshooting
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Order
 
Video Tutorials
Handbooks
MYOLP Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
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