Solved

Running a Function every 5 minutes

Posted on 2001-09-17
8
173 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 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…
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…

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