Results 1 to 3 of 3

Thread: Creating a Timer in Module

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2000
    Location
    Blairgowrie, Perthshire, Scotland
    Posts
    12
    Hi,

    I'm trying to create a Timer Control within a module so
    that it can be used in routines from any form.

    I have tried:

    Dim TestTmr As Timer

    Sub Form_Load
    TestTmr.Interval = 5000
    End Sub

    But this code causes an undefined object or missing with type error.

    How do I set up a Timer object in code rather than on a
    form?

    Any help greatly appreciated.

    Thanks
    James
    Dr. James Tweedie
    GeoMEM Consultants
    Scotland

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    You can use the SetTimer and KillTimer API's, ie.

    In a Module..
    Code:
    Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
    
    Private iTimerID As Long
    
    Public Sub StartTimer(ByVal Interval As Long)
        If iTimerID = 0 Then iTimerID = SetTimer(0&, 0&, Interval, AddressOf TimerCallback)
    End Sub
    
    Public Sub StopTimer()
        If iTimerID Then Call KillTimer(0&, iTimerID)
        iTimerID = 0
    End Sub
    
    Private Sub TimerCallback(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
        'Do whatever in the Timer Event Here.
    End Sub
    Usage:
    Code:
    Private Sub Form_Load()
        StartTimer 5000
    End Sub
    
    Private Sub Form_Unload()
        StopTimer
    End Sub

  3. #3
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    455
    Hello GeoMEM,

    Try this code:


    Declarations:
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


    Private Sub Form_Load()
    Call Sleep(5000)
    End Sub


    Good luck,

    Michelle.

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