Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Running a Function every 5 minutes

Posted on 2001-09-17
8
Medium Priority
?
181 Views
Last Modified: 2010-05-02
I have a function that I would like to be run every five minutes.  I'm pretty new to VB.  I think I probably have to use the Timer Control, but as far as actual code goes, I'm a little lost.  Any help please?
0
Comment
Question by:NJordan72
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6487533
You have two choices:

1) add a Timer to a form, and allow the application to run continuously, using the timer to determine the 5 minute interval.

2) use the Sleep API, have the application fire the function, and the Sepp for 5 minutes before firing the function again.

The choice is really up to you.
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 100 total points
ID: 6487538
Place the timer control on a form, set the interval property to 5000. This will fire approx every 5 seconds. This property has a max value of 65535 which is just over 1 minute.

Private Sub Timer1_Timer()
  Static LastRun As Date
  If DateDiff("n",LastRun,Now()) > 5 Then
    LastRun = Now()
    'Run your code
  End If
End Sub

This will check whether the last run was more than five minutes ago and will run your code if true. If false then 5 seconds or so later it will check again.
0
 
LVL 3

Expert Comment

by:SirNick
ID: 6487546
A timer control can only have an interval of about 1 min so you have to add more code. This should work, add a timer called 'timer1' and set the interval to 60000:

Dim tcounter As Integer
tcounter = "0"
Private Sub Timer1_Timer()
tcounter = tcounter + 1
If Counter <= "5" Then
    End Sub
ElseIf tcounter = "5" Then
    'do your function
    tcounter = "0"
End If
End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:NJordan72
ID: 6487552
So when a timer event is received the function Timer1_Timer() is automatically run?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6487564
Absolutely, when the interval expires the code in the event procedure is run.
0
 
LVL 3

Expert Comment

by:SirNick
ID: 6487565
A timer is like a clock with an alarm, if you set the interval to 1000 which is 1second it will run the code in timer1_timer() provided it is switched on (enabled = true)
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6487572
For the timer approach:

1) add a timer control to a form.


2)  set the Interval property of the timer to 10000 (10 seconds - interval is in MILLISECONDS)

add code like this to your form:
Option Explicit

Private Sub Form_Load()
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Static interval
   
    interval = interval + 1
    If interval = 30 Then
        ' fire you function here
        interval = 0
    End If
    Timer1.Enabled = True
End Sub
0
 

Author Comment

by:NJordan72
ID: 6487580
Thank you all very much!
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question