?
Solved

taking a break...

Posted on 1999-01-25
14
Medium Priority
?
206 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
[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
  • 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
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!

 
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
 

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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…
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…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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