Solved

Very simple Question...Ans Pls

Posted on 2003-12-01
4
357 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 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

690 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