Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Using the timer control

Posted on 2002-07-30
Medium Priority
862 Views
I need have a function that I need to run at a certain interval i.e 5 mins. I have not used the timer control before and I am looking for some code to wait a certain amount of time before running a procedure.
0
Question by:SammyG
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3
• 2
• +3

LVL 4

Accepted Solution

Glowman earned 200 total points
ID: 7187801
Sammy,
Place the Timer control on your form and set the Interval property to 1000(1000 is one second ) and the enabled property to True.  Then in the Timer1_Timer event you will write the code that will count to 300 since there are 300 seconds in five minutes then execute your procedure, then toggle the enabled property of the timer to false, unless you want your procedure to run every five minutes.  An example is as follows.

Option Explicit
Public TimeWatch As Integer
Private Sub Timer1_Timer()

If TimeWatch = 301 Then
Call MySubRoutine
Timer1.Enabled = False
Else
TimeWatch = TimeWatch + 1
End If

End Sub

Hope this gets you started
G
0

Author Comment

ID: 7187841

Here is how I need this to work:

Sub DoSomething()

If CanContinue() = False Then
Wait 5 mins and run DoSomething Again
Else
'Carry on
End If

End Sub

Function CanContinue() as Boolean
If ...'Do something
CanContinue = True
Else
CanContimue = False
End If
End Function
0

Author Comment

ID: 7187851

Here is how I need this to work:

Sub DoSomething()

If CanContinue() = False Then
Wait 5 mins and run DoSomething Again
Else
'Carry on
End If

End Sub

Function CanContinue() as Boolean
If ...'Do something
CanContinue = True
Else
CanContimue = False
End If
End Function
0

LVL 4

Expert Comment

ID: 7187868
Sammy,

You can do it that way by declaring a boolean value and changing it to True in the Timer1_timer event in place of where we called MySubRoutine.  So then your code you could have a tight loop that waits for the boolean value to become true, like this.

Option Explicit
Public TimeWatch As Integer
public TimeToGo as Boolean
Private Sub Timer1_Timer()

If TimeWatch = 300 Then
TimeToGo=True
Else
TimeWatch = TimeWatch + 1
End If

End Sub

Sub DoSomething()

If CanContinue() = False Then
Wait 5 mins and run DoSomething Again

Do until TimeToGo= true
DoEvents
Loop

TimeWatch=0
TimeToGo=False

Else
'Carry on
End If

End Sub

So we are waiting for TimeToGo to be switched to True when the Timer hits 300, and after it does reset the TimeWatch Value back to Zero and TimeToGo to False.  Better?
0

LVL 4

Expert Comment

ID: 7187894
Sammy, if you don't use a timer, then your program will hog the processor and appear to be frozen/hung - both of which are "very bad things"(tm)

If you're determined to go down this route, then try the Sleep API command:

Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

eg:
If CanContinue() = False Then
Sleep 3000000   ' = 5 mins in milliseconds

0

Author Comment

ID: 7187971
Nazdor:

I currently am using the sleep API. Problem is I would like to show a countdown to the user with an option to cancel.
0

LVL 28

Expert Comment

ID: 7187980
I would recommend using SetTimer and KillTimer API's for this.Here is how,just change the time interval in the example to 300000 for 5 minutes.

http://www.vbcodemagician.dk/tips/system_formlesstimer.htm

This is a better then using the timer control but remember,you must use killTimer to stop the timer as stated in the link.
0

LVL 4

Expert Comment

ID: 7187984
Sammy,

Then you will have to use the timer control.  The sleep function locks the app without any refreshing.

G
0

LVL 2

Expert Comment

ID: 7188141
Hi,

Try this

Timer1 Properties
================
Interval=5000 (1000 for 1 sec)
enabled=true

private sub message()''the procedure to be called
msgbox "Message"
end sub

private sub timer1_timer()
call message
timer1.enabled=false'if u want to stop executing

'u dont need the above line if u want the code to fire 'for every 5 min

end sub
0

LVL 4

Expert Comment

ID: 7188312
Sounds like you need to display a small Modal form with a cancel button on (plus other info, such as your countdown) in conjuction with timers.

If you create the wait form with events, then you could raise an event to the parent form when the user presses cancel.

SetTimer/KillTimer, though handy in some cases, is fairly immaterial in this case as the ocx timer does the job without all the hassle of a timer in a global .bas which may need a reference to individual objects etc etc etc

0

LVL 28

Expert Comment

ID: 7188335
>>> SetTimer/KillTimer, though handy in some cases, is fairly immaterial in this case

I totally disagree with this statement.
0

Expert Comment

ID: 7188337
I think you will be limited to 1 minute using the Timer control, you could try the Timer APIs if you need longer intervals:

API Declarations:
Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long

Public Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long

The timer will call the specified function at the specified interval.

Eg.
g_lTimerId = SetTimer(0, 0, (lLastTxnQueryInterval * 60000), AddressOf TimerProc)

Here TimerProc will be called every lLastTxnQueryInterval (this variable holds the interval in minutes - so we need to convert it to msecs).

To stop the timer simply call - KillTimer(0, g_lTimerId)

The TimerProc definition looks like:
Public Sub TimerProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)

Hope this helps.

0

LVL 4

Expert Comment

ID: 7206837
ummm Sammy???
You still alive?
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't interesteâ€¦
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describesâ€¦
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). Uâ€¦
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This lâ€¦
###### Suggested Courses
Course of the Month12 days, 6 hours left to enroll