Monday, August 18, 2008

Visual Basic 6: How to create a delay using the Sleep API Call

Note: The following applies to both Visual Basic 6 and VBA.

Sometimes you just need to put your application to sleep for a little while such as when you need to build in a delay while other applications do their thing. There are a couple of ways you can accomplish this but the easiest is to call on the Sleep API.

Enter the following in the General Declarations section of a form or module.

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

To use: just add the line Sleep X to your code where X is the number of milliseconds of delay that you want to build in. Works like a charm.

No comments: