How To set timer in VB6
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: How To set timer in VB6

Hybrid View

  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
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,838

    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,091

    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
    Chicago, IL
    Posts
    14,982

    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
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,838

    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.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center