CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2011
    Posts
    9

    How To set timer in VB6

    I want to use the timer function to get latest information from database
    if it's more than 1 minute then the function will get the latest data and update the text box.
    Pls guide me how to do this.

    Thanks!

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How To set timer in VB6

    Timer interval is in milliseconds. To have code execute 1 time each minute place a timer on your form, set the timer interval to 60,000, set the enabled property to true and place a call to your code in the timer tick event.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: How To set timer in VB6

    Depending on the amount of processing the Timer event must do, setting it for each minute can cause some really sluggish ( slow ) applications, be careful if that is the case

  4. #4
    Join Date
    Jun 2011
    Posts
    9

    Re: How To set timer in VB6

    My interval should be between 5-10 min

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How To set timer in VB6

    Best to use a ONE SECOND (or so) timer, and count the CLICKS
    It counts UP for a minute...

    Code:
    Option Explicit
    ' Add a Timer control to your project.  It will be Timer1
    ' It looks like a stop watch in the IDE.
    Dim OldTime As Date
    Dim newTime As Date
    Dim diff As Date
    
    Private Sub Form_Load()
      OldTime = Time
      Timer1.Interval = 1000
      Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
      Static x As Long
      Static zz$, ss$
      x = x + 1
      newTime = Time
      diff = DateDiff("s", OldTime, newTime)
      Form1.Caption = (diff \ 3600) & ":" & Format((diff \ 60 Mod 60), "00") & ":" & _
            Format((diff - ((diff \ 60) * 60)), "00")
    '  Debug.Print (diff \ 3600) & ":" & Format((diff \ 60 Mod 60), "00") & ":" & _
            Format((diff - ((diff \ 60) * 60)), "00")
      If newTime = DateAdd("s", 360, OldTime) Then ' Add 360 seconds
        Timer1.Enabled = False
        ' You time is UP!  Do something!
        Beep
      End If
    End Sub
    Note to self: Reinstall VB6... Knew I forgot something...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How To set timer in VB6

    I would disagree, True it will fire every minute but it is also firing 59 times in between and doing some processing each time. This is not a big deal but getting in a habit of writing such code will result in programs running slower than they should.

    If you need it to fire every 5 to 10 minutes I would set the interval to 1 minute and use a counter to see how many minutes have elapsed using a static var with a counter as is the case in the example above with X though X is not used in the code just as a wasted process.

    In your timer routine
    Code:
    Static x As Integer
    x=x+1
    If X> 5 then
       X=0
       'Do your processing
    End IF
    Last edited by DataMiser; June 9th, 2011 at 10:46 AM.
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    May 2019
    Posts
    1

    Re: How To set timer in VB6

    Quote Originally Posted by dglienna View Post
    Best to use a ONE SECOND (or so) timer, and count the CLICKS
    It counts UP for a minute...

    Code:
    Option Explicit
    ' Add a Timer control to your project.  It will be Timer1
    ' It looks like a stop watch in the IDE.
    Dim OldTime As Date
    Dim newTime As Date
    Dim diff As Date
    
    Private Sub Form_Load()
      OldTime = Time
      Timer1.Interval = 1000
      Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
      Static x As Long
      Static zz$, ss$
      x = x + 1
      newTime = Time
      diff = DateDiff("s", OldTime, newTime)
      Form1.Caption = (diff \ 3600) & ":" & Format((diff \ 60 Mod 60), "00") & ":" & _
            Format((diff - ((diff \ 60) * 60)), "00")
    '  Debug.Print (diff \ 3600) & ":" & Format((diff \ 60 Mod 60), "00") & ":" & _
            Format((diff - ((diff \ 60) * 60)), "00")
      If newTime = DateAdd("s", 360, OldTime) Then ' Add 360 seconds
        Timer1.Enabled = False
        ' You time is UP!  Do something!
        Beep
      End If
    End Sub
    Note to self: Reinstall VB6... Knew I forgot something...

    Hi, can you help me to modify your this code to get the timing information in Milliseconds as well please?

Tags for this Thread

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

Featured