Animating a Picture

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AI Man
    New Member
    • Apr 2007
    • 5

    #16
    Here are some ways to time things out in VB.

    In this instance the program will not run until after 7 AM.

    Do
    If Time > #7:00:00 AM# Then
    MsgBox ("Time is " & Time)
    Exit Do
    End If
    Loop


    This one simply delays a process for a set amount of time. This example would delay the process for 1 asecond. I am pulling it off the top of my head so it might not be exact.

    Application.wai tnow + (timevalue("00: 00:01"))


    There is also the timer function from Excel Help that can be used in Access with minor modifications.

    This example uses the Timer function to pause the application. The example also uses DoEvents to yield to other processes during the pause.

    Dim PauseTime, Start, Finish, TotalTime
    If (MsgBox("Press Yes to pause for 5 seconds", 4)) = vbYes Then
    PauseTime = 5 ' Set duration.
    Start = Timer ' Set start time.
    Do While Timer < Start + PauseTime
    DoEvents ' Yield to other processes.
    Loop
    Finish = Timer ' Set end time.
    TotalTime = Finish - Start ' Calculate total time.
    MsgBox "Paused for " & TotalTime & " seconds"
    Else
    End
    End If



    And one last timer function for VBA taken from http://msdn2.microsoft .com/en-us/library/aa211461(office .11).aspx

    Private Sub Form_Timer()
    Clock.Caption = Time ' Update time display.
    End Sub

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #17
      Originally posted by AI Man
      Here are some ways to time things out in VB.

      In this instance the program will not run until after 7 AM.

      Do
      If Time > #7:00:00 AM# Then
      MsgBox ("Time is " & Time)
      Exit Do
      End If
      Loop
      Thanks for the input. I would caution readers, though, that this is a good way to chew up heaps of CPU and slow things down. I'd suggest including a DoEvents statement inside the loop, to tell Windows it can go do other things.

      Of course, this is less of a problem than it was under earlier versions of Windows, where you could completely lock up the system.

      Originally posted by AI Man
      This one simply delays a process for a set amount of time. This example would delay the process for 1 asecond. I am pulling it off the top of my head so it might not be exact.

      Application.wai tnow + (timevalue("00: 00:01"))
      According to the online help here, the syntax is...
      expression.Wait(Time)
      The examples given are:
      Example
      This example pauses a running macro until 6:23 P.M. today.

      Code:
      Application.Wait "18:23:00"
      This example pauses a running macro for approximately 10 seconds.

      Code:
      newHour = Hour(Now())
      newMinute = Minute(Now())
      newSecond = Second(Now()) + 10
      waitTime = TimeSerial(newHour, newMinute, newSecond)
      Application.Wait waitTime

      Originally posted by AI Man
      There is also the timer function from Excel Help that can be used in Access with minor modifications.

      This example uses the Timer function to pause the application. The example also uses DoEvents to yield to other processes during the pause.

      Code:
      Dim PauseTime, Start, Finish, TotalTime
      If (MsgBox("Press Yes to pause for 5 seconds", 4)) = vbYes Then
          PauseTime = 5    ' Set duration.
          Start = Timer    ' Set start time.
          Do While Timer < Start + PauseTime
              DoEvents    ' Yield to other processes.
          Loop
          Finish = Timer    ' Set end time.
          TotalTime = Finish - Start    ' Calculate total time.
          MsgBox "Paused for " & TotalTime & " seconds"
      Else
          End
      End If
      Nice one.

      I do have a minor quibble, though. For efficiency reasons, it's generally not a good idea to include unnecessary calculations in a loop. In this example, you really should calculate Start + PauseTime once up front, store the result in a variable, then use that variable in the While condition.

      What I generally do when I want a pause like this is to put it in a sub or function, so I can call it from more than one place. It also makes you code more readable, since you can just say something like:
      Pause 3

      Originally posted by AI Man
      ...Private Sub Form_Timer()
      Clock.Caption = Time ' Update time display.
      End Sub
      Good point. I was forgetting that a form has its own OnTimer event, and TimerInterval property. In Access, at least. Does this apply in Excel? I don't think I've ever used a form in Excel.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #18
        Originally posted by I Hate My Computer
        This didn't do anything. No error, it just didn't do anything.
        Generally speaking, I would avoid using delay loops. When processors ran at 1MHz they were useful. These days there is just too much variation, and too much speed. A loop that causes a one-second delay today, might produce no noticeable delay at all after the next upgrade.

        Better to use one of the clock-based delay techniques as discussed in other messages here.

        Comment

        Working...