?
Solved

Very simple Question...Ans Pls

Posted on 2003-12-01
4
Medium Priority
?
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 39

Accepted Solution

by:
appari earned 200 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 86

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

650 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