Solved

taking a break...

Posted on 1999-01-25
14
195 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
Technology Partners: 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 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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Adding to a VBA? 6 84
Access 2013 combo box not working 3 58
Copy a range from 1..n excel sheets to one destination sheet 2 66
VBA - If Bookmark = "XXBOOKMARKXX" then 15 50
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

735 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