-
May 26th, 2024, 04:39 PM
#1
Thread Starter
PowerPoster
[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.
-
May 26th, 2024, 05:04 PM
#2
Addicted Member
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
-
May 26th, 2024, 05:27 PM
#3
Re: Start my private sub at every 2 minute...
Max value for Interval is 65535...
-
May 27th, 2024, 12:52 AM
#4
Re: Start my private sub at every 2 minute...
 Originally Posted by VanGoghGaming
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
-
May 27th, 2024, 03:24 AM
#5
Thread Starter
PowerPoster
Re: Start my private sub at every 2 minute...
 Originally Posted by Zvoni
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)
-
May 27th, 2024, 03:51 AM
#6
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
-
May 27th, 2024, 11:53 AM
#7
Addicted Member
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
-
May 28th, 2024, 08:02 AM
#8
Hyperactive Member
Re: Start my private sub at every 2 minute...
 Originally Posted by Joe Caverly
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
-
May 28th, 2024, 08:54 AM
#9
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.

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.
-
May 28th, 2024, 09:07 AM
#10
Thread Starter
PowerPoster
Re: Start my private sub at every 2 minute...
 Originally Posted by joe caverly
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!
-
May 28th, 2024, 09:08 AM
#11
Thread Starter
PowerPoster
Re: Start my private sub at every 2 minute...
 Originally Posted by elroy
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|