Results 1 to 11 of 11

Thread: [RESOLVED] Start my private sub at every 2 minute...

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,733

    Resolved [RESOLVED] Start my private sub at every 2 minute...

    I need to start at every 2 minute a private sub stored in a form1.
    How to?
    Never used.

  2. #2
    Addicted Member
    Join Date
    May 2012
    Location
    42.787034,-81.176367
    Posts
    152

    Re: Start my private sub at every 2 minute...

    Use a timer control to execute a subroutine (sub) at regular intervals.

    Add a Timer Control to Your Form
    Open your VB6 project.

    From the toolbox, drag and drop a Timer control onto your form.

    Double-click on the Timer icon to open the code window for the Timer1_Timer() event

    Set the Timer Interval
    In the Timer1_Timer() event,
    specify the code you want to run every 2 minutes.

    Set the Interval property of the Timer control to the desired interval in milliseconds.

    For 2 minutes,
    set it to 120000 (2 minutes × 60 seconds/minute × 1000 milliseconds/second).

    Example Code:
    Code:
    Private Sub Form_Load()
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
        ' Your code here (e.g., call your subroutine)
        Call YourSubroutine
    End Sub
    Joe

  3. #3

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,968

    Re: Start my private sub at every 2 minute...

    Quote Originally Posted by VanGoghGaming View Post
    Max value for Interval is 65535...
    Then instead inside the Timer-Event poll the Tickcount every 1000 ms comparing it to a StartTickCount...
    easy....
    maybe not the most accurate way, but it gets the job done
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,733

    Re: Start my private sub at every 2 minute...

    Quote Originally Posted by Zvoni View Post
    Then instead inside the Timer-Event poll the Tickcount every 1000 ms comparing it to a StartTickCount...
    easy....
    maybe not the most accurate way, but it gets the job done
    tks bro.

    example please based every 2 minutes. (StartTickCount)

  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,418

    Re: Start my private sub at every 2 minute...

    Just use a static variable "Count" in de Timer event
    Set the Timer interval to 10000 ms -> 10 seconds
    Then increase Count till it reaches 12 (12 * 10 seconds = 2 minutes)
    When 12 execute your desired code and set the counter back to 0

  7. #7
    Addicted Member
    Join Date
    May 2012
    Location
    42.787034,-81.176367
    Posts
    152

    Re: Start my private sub at every 2 minute...

    Adapted from: https://www.vbforums.com/showthread....=1#post3526372

    Code:
    Option Explicit
    
    Public NumMins
    
    Private Sub Form_Load()
      NumMins = 0
      
      'set the timer's Interval to 60,000 (to be 1 minute)
      Timer1.Interval = 60000
    End Sub
    
    ' in your Timer event
    Private Sub Timer1_Timer()
       NumMins = NumMins + 1
       If NumMins = 2 Then
          NumMins = 0   ' to reset
          '<your code here>
       End If
    End Sub
    Joe

  8. #8
    Hyperactive Member gaouser's Avatar
    Join Date
    Mar 2022
    Location
    World:\Turkey\User32.DLL
    Posts
    510

    Re: Start my private sub at every 2 minute...

    Quote Originally Posted by Joe Caverly View Post
    Adapted from: https://www.vbforums.com/showthread....=1#post3526372

    Code:
    Option Explicit
    
    Public NumMins
    
    Private Sub Form_Load()
      NumMins = 0
      
      'set the timer's Interval to 60,000 (to be 1 minute)
      Timer1.Interval = 60000
    End Sub
    
    ' in your Timer event
    Private Sub Timer1_Timer()
       NumMins = NumMins + 1
       If NumMins = 2 Then
          NumMins = 0   ' to reset
          '<your code here>
       End If
    End Sub
    Joe
    If you didnt understand, in Joe's code
    2 minutes = 120 Secs = 120000 ms, This exceeds integer limit, the Interval is integer. but a min can be in an integer. So Joe resets timer every 2 ticks. resulting in 2 minutes timer that ticks per 2 mins without ability to know how muchs been since started
    MicrosoftWindowsxp
    Professional

    was the peak Windows version not gonna lie

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,738

    Re: Start my private sub at every 2 minute...

    Ok, let's get some of this straightened out.

    First, let's get that NumMins inside the timer event as a local variable. And, for goodness sake, let's declare it as a non-Variant.

    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
      ' set the timer's Interval to 60,000 (to be 1 minute)
      Timer1.Interval = 60000
      Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
        Static NumMins As Long  ' No need to initialize this, as we can trust that it starts out zero.
        NumMins = NumMins + 1&
        If NumMins = 2& Then
            NumMins = 0&
    
    
            '<your code here>
    
    
        End If
    End Sub
    
    
    Next, the timer's Interval property is a Long, not an Integer.

    Name:  TimerInterval.png
Views: 153
Size:  18.1 KB

    Sure, it might be a ushort (aka, uint16) somewhere under the hood. But, strictly speaking, it's assigned and returned as a Long type to us. And, upon being assigned, it's checked for bounds.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,733

    Re: Start my private sub at every 2 minute...

    Quote Originally Posted by joe caverly View Post
    adapted from: https://www.vbforums.com/showthread....=1#post3526372

    Code:
    option explicit
    
    public nummins
    
    private sub form_load()
      nummins = 0
      
      'set the timer's interval to 60,000 (to be 1 minute)
      timer1.interval = 60000
    end sub
    
    ' in your timer event
    private sub timer1_timer()
       nummins = nummins + 1
       if nummins = 2 then
          nummins = 0   ' to reset
          '<your code here>
       end if
    end sub
    joe
    tks bro!

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,733

    Re: Start my private sub at every 2 minute...

    Quote Originally Posted by elroy View Post
    ok, let's get some of this straightened out.

    First, let's get that nummins inside the timer event as a local variable. And, for goodness sake, let's declare it as a non-variant.

    Code:
    
    option explicit
    
    private sub form_load()
      ' set the timer's interval to 60,000 (to be 1 minute)
      timer1.interval = 60000
      timer1.enabled = true
    end sub
    
    private sub timer1_timer()
        static nummins as long  ' no need to initialize this, as we can trust that it starts out zero.
        nummins = nummins + 1&
        if nummins = 2& then
            nummins = 0&
    
    
            '<your code here>
    
    
        end if
    end sub
    
    
    next, the timer's interval property is a long, not an integer.

    Name:  TimerInterval.png
Views: 153
Size:  18.1 KB

    sure, it might be a ushort (aka, uint16) somewhere under the hood. But, strictly speaking, it's assigned and returned as a long type to us. And, upon being assigned, it's checked for bounds.
    tks bro

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