Solved

taking a break...

Posted on 1999-01-25
14
182 Views
Last Modified: 2010-05-03
What would be the easiest way to take break, ex. 1 second, in middle of a code?? I need a sub that I can call in a middle of a code (Call Break()).
0
Comment
Question by:majala
  • 7
  • 4
  • 3
14 Comments
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1470475
Put this in your code:

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

Sleep 1000 ' 1 seconds
0
 

Author Comment

by:majala
ID: 1470476
I putted that "private Declare Sub..." in a forms General area (because otherwise i got an error) and then called it just like you showed "Sleep 1000", but it didn't do anything? I tryed also Sleep(1000).
0
 
LVL 18

Expert Comment

by:deighton
ID: 1470477
'EXAMPLE FOR 10 SECOND DELAY    

    Dim tNow As Date
   
   
    tNow = Now
   
    While DateDiff("s", tNow, Now) < 10
    DoEvents
    Wend
   
    MsgBox "!"

0
 
LVL 18

Expert Comment

by:deighton
ID: 1470478
'OR ANOTHER VERSION - IDEAL FOR SHORT DELAYS LIKE ONE SECOND

Private Sub Command1_Click()

    Delay (1)
    MsgBox "!"

End Sub


Public Function Delay(nSeconds As Double)

    Dim n As Double
    Dim t As Double
   
   
    n = Timer
   
   
    Do
   
        t = Timer
        If t < n Then t = t + 24 * 3600
        DoEvents
       
    Loop Until t - n > nSeconds
   


End Function

0
 

Author Comment

by:majala
ID: 1470479
I putted that "private Declare Sub..." in a forms General area (because otherwise i got an error) and then called it just like you showed "Sleep 1000", but it didn't do anything? I tryed also Sleep(1000).
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1470480
Add a button to a form
and insert this code on the top of the form

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


Put this code where you need it, example:

Private Sub Command1_Click()
   Sleep 1000 ' 1 seconds
end sub
0
 

Author Comment

by:majala
ID: 1470481
That your comment worked fine Deighton! I don't remember rejecting your first answer!?
Anyway if you could tell me what I have to add to code if I wanna shorter breaks than one second? So I could call it like:
Delay(100), and it would mean one second delay.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:majala
ID: 1470482
Mirkwood: I did it just like you said, but it didn't work anyway!
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1470483
Are you using VB3 or VB4?
0
 

Author Comment

by:majala
ID: 1470484
I'm using VB5.
0
 

Author Comment

by:majala
ID: 1470485
I'm using VB5.
0
 
LVL 18

Expert Comment

by:deighton
ID: 1470486
Public Function Delay(nMilliSeconds As Double)

    'This can be used with delays of .1 or .25 or whatever
    'The accuracy is limited to the tick rate of the clock
    '8 per second I believe.
   
    'This code copes with the midnight boundary where timer reverts to 0


    Dim n As Double
    Dim t As Double
    Dim nSeconds As Double
   
    nSeconds = nMilliSeconds / 1000
   
   
    n = Timer
   
   
    Do
   
        t = Timer
        If t < n Then t = t + 24 * 3600
        DoEvents
       
    Loop Until t - n > nSeconds
   

End Function


0
 

Author Comment

by:majala
ID: 1470487
Deighton: Now it works just like I want it to work. You can leave your comment as an answer...
0
 
LVL 18

Accepted Solution

by:
deighton earned 50 total points
ID: 1470488


          'This can be used with delays of .1 or .25 or whatever
          'The accuracy is limited to the tick rate of the clock
          '8 per second I believe.
           
          'This code copes with the midnight boundary where timer reverts to 0


          Dim n As Double
          Dim t As Double
          Dim nSeconds As Double
           
          nSeconds = nMilliSeconds / 1000
           
           
          n = Timer
           
           
          Do
           
              t = Timer
              If t < n Then t = t + 24 * 3600
              DoEvents
               
          Loop Until t - n > nSeconds
           

      End Function
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 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

19 Experts available now in Live!

Get 1:1 Help Now