Solved

Running a Function every 5 minutes

Posted on 2001-09-17
8
174 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 25 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

821 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