Results 1 to 16 of 16

Thread: Loop Delay

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    1

    Loop Delay

    Hi, not sure if this is the right section, so apologises if it's wrong.

    I'm coding in Visual Basic in Microsoft Access 2003, trying to make a simple program for a School project.

    Basically, I have written a loop so on the click of a button the number in a text box goes from 0 to 100, the only problem is it moves so fast you can't see it and just jumps from 0 to 100 without the numbers inbetween coming up. Is it possible to add a delay or pause to slow down the transition ?

    Thanks.

  2. #2
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Loop Delay

    Add a timer control, use 1000 (or 1 second) as the interval, and have that timer increment the value.

  3. #3
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Loop Delay

    Oh, and just thought of this:

    Alternatively, if you can't load a timer, then this sub-routine will work (but once again, uses seconds):

    Code:
    Public Sub Delay(ByVal Num As Integer)
    
    Dim dteTemp As Date
    
    dteTemp = Now + Second(Num)
    
    Do Until Now = dteTemp
        DoEvents
    Loop
    
    End Sub

  4. #4
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Loop Delay

    Welcome to the Forums!!!


    Or use sleep API

    Code:
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Code:
    For i = 0 to 100
      Text1.text = i
      sleep 200
    Next
    IIF(Post.Rate > 0 , , )

  5. #5
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Loop Delay

    Quote Originally Posted by zeezee
    Welcome to the Forums!!!


    Or use sleep API

    Code:
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Code:
    For i = 0 to 100
      Text1.text = i
      sleep 200
    Next
    The above code will not do what you want unless you add a DoEvents command to the sub as shown below. It will simply delay the loop execution for 200 mS and then race through the loop. Btw, I don't like the Sleep API, as it suspends execution of all code and events in your app for the Sleep period. The Delay Sub that was posted earlier by Campion is a better choice.

    Code:
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Private Sub Command2_Click()
       For i% = 0 To 10
          Sleep 200
             Text1.Text = i%
          DoEvents
       Next
    End Sub
    Here's some code that increments the count by use of a Timer control.

    Code:
    Private Sub Command1_Click()
       Timer1.Interval = 200           
       Timer1.Enabled = True
    End Sub
    
    
    Private Sub Form_Load()
       Timer1.Enabled = False
    End Sub
    
    Private Sub Timer1_Timer()
       Static i%
          i% = i% + 1
             Text1.Text = i%
          If i% >= 100 Then
             Timer1.Enabled = False
          End If
    End Sub
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  6. #6
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Loop Delay

    I try not to use timers whenever possible, but this really is a situation for a timer.

  7. #7
    New Member
    Join Date
    Sep 2007
    Posts
    13

    Re: Loop Delay

    Hi, i pretty much need the same thing. I too am working with Access.

    Code:
    Public Sub Delay(ByVal Num As Integer)
    
    Dim dteTemp As Date
    
    dteTemp = Now + Second(Num)
    
    Do Until Now = dteTemp
        Text100 = Text100 + 0.01
    Loop
    
    End Sub
    Then i have 2 buttons, start and stop. When the start button is clicked i added the "Delay (5)" in it but it doesn't seem to be running the delay.

    Any ideas why?

    Also if the delay does work and say i put a delay of 100 (so it increases by 0.01 for 100 seconds (i think?)) when i press the stop button will the delay loop automatically stop?

    Thanks in advance.

  8. #8
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Loop Delay

    Quote Originally Posted by Muncey
    Hi, i pretty much need the same thing. I too am working with Access.

    Then i have 2 buttons, start and stop. When the start button is clicked i added the "Delay (5)" in it but it doesn't seem to be running the delay.

    Thanks in advance.
    Muncey--

    Do not put the code that you wish to delay INSIDE of the delay routine; otherwise, it will NOT slow down execution, and it will NOT delay. The purpose of the routine is to DELAY execution of the routine that calls it. Leave it as I had written it (with one minor change, posted next.)

    Code:
    Do Until Now >= dteTemp
    You should call this routine inside of a loop so that it will increment, then wait, then increment, then wait.

  9. #9
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Loop Delay

    Try this code. It should work fine. You can change the Pause interval if you want.

    Code:
    Option Explicit
    
    Private Sub Form_Load()
       Show
       Dim i%
          For i% = 1 To 10
             Print i%
            Pause 1                       'pause for 1 sec
          Next
    End Sub
    
    
    Private Sub Pause(ByVal Delay As Single)
        Dim x As Single
        x = Timer + Delay                  ' Add a delay to the current time
        Do While x > Timer                 ' and waits for the current time
            DoEvents                       ' to catch up.
        Loop
    End Sub
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  10. #10
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Loop Delay

    Quote Originally Posted by Muncey
    Hi, i pretty much need the same thing. I too am working with Access.

    Then i have 2 buttons, start and stop. When the start button is clicked i added the "Delay (5)" in it but it doesn't seem to be running the delay.

    Any ideas why?

    Also if the delay does work and say i put a delay of 100 (so it increases by 0.01 for 100 seconds (i think?)) when i press the stop button will the delay loop automatically stop?

    Thanks in advance.
    You can enter and exit the loop like this:

    Code:
    Option Explicit
    Dim blnFlag As Boolean
    
    Private Sub Form_Load()
       Show
       Me.Caption = "Pause Sub Demo"
       Command1.Caption = "Run Loop"
       Command2.Caption = "Exit Loop"
    End Sub
    
    Private Sub Command1_Click()
      Dim i%
          For i% = 1 To 10
             Print i%
                Pause (1)                       ' Call Pause sub. Pause period = 1 sec.
             If blnFlag = True Then
                Print "User clicked  Exit Loop."
                Exit For           ' If Cmd2 was clicked exit loop
             End If
          Next
    End Sub
    
    Private Sub Command2_Click()
        blnFlag = True                    'Set blnFlag to True
    End Sub
    
    Private Sub Pause(ByVal Delay As Single)
        Dim x As Single
        x = Timer + Delay                  ' Add a delay to the current time
        Do While x > Timer                 ' and waits for the current time
            DoEvents                       ' to catch up.
        Loop
    End Sub
    Last edited by CDRIVE; Mar 13th, 2008 at 09:14 PM. Reason: append
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Loop Delay

    Following code is CPU intensive
    Code:
    Private Sub Pause(ByVal Delay As Single)
        Dim x As Single
        x = Timer + Delay                  ' Add a delay to the current time
        Do While x > Timer                 ' and waits for the current time
            DoEvents                       ' to catch up.
        Loop
    End Sub
    If possible use a timer to avoid intensive use of CPU. Simply enable/disable timer to start/stop processing. Maintain module level variables for timer call to know where it is in the process, timer event simply picks up module level values and updates textbox accordingly. Loops require DoEvents or Text1.Refresh to update the GUI, timer does not. Limitation of timer is the interval (precision and max)... but if you are using ~20 milliseconds or more as delay then this shouldn't be an issue.

    If timer is not feasible and you will have to loop then use Sleep which is also not CPU intensive. You will need Text1.Refresh to update GUI.

    Constructs that are loop intensive and use DoEvents is more common to graphics (e.g. DirectX), those requiring small millisecond delays such as performance measuring apps, and other similar implementations (deliberately CPU and other resources use intensive such as high-end games) ... it shouldn't be used in simple requirements such as thread starter's as it encourages a bad programming habit and will spawn programs that act as if they have exclusive use of resources.
    Last edited by leinad31; Mar 13th, 2008 at 10:21 PM.

  12. #12
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Loop Delay

    Here's one way:

    Code:
    Private Sub Command1_Click()
    Dim x as integer
    for x = 0 to 100
    doevents:refresh
    text1.text=x
    next x
    end sub
    Using a Timer:
    Code:
    Dim x as integer
    Private Sub Timer1_Timer()
    if x = 100 then timer1.enabled=false:exit sub
    text1.text=x
    x=x+1
    doevents:refresh
    end sub

  13. #13
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Loop Delay

    Quote Originally Posted by leinad31
    Following code is CPU intensive
    Code:
    Private Sub Pause(ByVal Delay As Single)
        Dim x As Single
        x = Timer + Delay                  ' Add a delay to the current time
        Do While x > Timer                 ' and waits for the current time
            DoEvents                       ' to catch up.
        Loop
    End Sub
    If timer is not feasible and you will have to loop then use Sleep which is also not CPU intensive. You will need Text1.Refresh to update GUI.
    No argument here. The Pause Sub is CPU intensive, but as I stated earlier in this thread, the Sleep API halts ALL execution of code and events in your app, not just the code block you want to delay. The Pause Sub does not. Other parts of your code & events can still execute.
    Last edited by CDRIVE; Mar 24th, 2008 at 09:07 PM. Reason: Clarity
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  14. #14
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Loop Delay

    Then app should have been designed accordingly from the start to implement scheduled task or to be trigger/event based. Output oriented development can only go so far, what you don't see can actually hurt.

  15. #15
    New Member
    Join Date
    Sep 2007
    Posts
    13

    Re: Loop Delay

    Quote Originally Posted by CDRIVE
    Try this code. It should work fine. You can change the Pause interval if you want.

    Code:
    Option Explicit
    
    Private Sub Form_Load()
       Show
       Dim i%
          For i% = 1 To 10
             Print i%
            Pause 1                       'pause for 1 sec
          Next
    End Sub
    
    
    Private Sub Pause(ByVal Delay As Single)
        Dim x As Single
        x = Timer + Delay                  ' Add a delay to the current time
        Do While x > Timer                 ' and waits for the current time
            DoEvents                       ' to catch up.
        Loop
    End Sub
    This worked brilliantly, thanks a lot!

    Thanks to everybody else for the help!

  16. #16
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Loop Delay

    Quote Originally Posted by Muncey
    This worked brilliantly, thanks a lot!

    Thanks to everybody else for the help!
    You're welcome but keep in mind it is CPU intensive so avoid long delay periods. The bottom end of this sub is Pause (0.005) ' pause for 5.0mS.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width