Solved

Very simple Question...Ans Pls

Posted on 2003-12-01
4
352 Views
Last Modified: 2010-05-01
Hi,
How to insert the time delay between two statement execution in VB

I know in "C" it is "delay(<time in milliseconds>)"
But in VB.....?
Bye
Prakashbe
0
Comment
Question by:prakashbe
4 Comments
 
LVL 39

Accepted Solution

by:
appari earned 50 total points
ID: 9855929
use sleep API

add the API declaration
Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

call it like
sleep 10000 '**** Sleeps for 10 Sec.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9856091
The Sleep() API will delay for the specified time but completely suspends execution of your application making it unresponsive.  

If you need your application to remain responsive during the delay then you can do it this way:

Private Sub delay(seconds As Long)
    Dim endTime As Date
   
    endTime = DateAdd("s", seconds, Now())
    Do While Now() < endTime
        DoEvents
    Loop
End Sub

To demonstrate the difference, create a new project and add two commandButtons.  Hit the first button and then try moving the window around or minimizing and the maximizing it again.  A msgbox will pop up after ten seconds.

Now hit the second commandbutton and try the same thing.  Your app won't respond until the ten seconds are up and the msgbox has been dismissed.

Regards,

Idle_Mind

' Code Follows...
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Command1_Click()
    delay 10
    MsgBox "done"
End Sub

Private Sub delay(seconds As Long)
    Dim endTime As Date
   
    endTime = DateAdd("s", seconds, Now())
    Do While Now() < endTime
        DoEvents
    Loop
End Sub

Private Sub Command2_Click()
    Sleep 10000
    MsgBox "done"
End Sub
0
 
LVL 14

Expert Comment

by:aelatik
ID: 9856127
The Sleep function freezes your application and won't respond to anything until the time has passed. You can use another method which won't freeze up your application :


Private Sub Form_Load()
Dim T As Double
    T = Timer
    Do Until CLng(Timer - T) = 5
        DoEvents
    Loop
        MsgBox "Finsihed"
End Sub
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9857749
sleep has same effect as delay in c++

Don't accept this as answer
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now