Solved

Running a Function every 5 minutes

Posted on 2001-09-17
8
171 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
 

Author Comment

by:NJordan72
ID: 6487552
So when a timer event is received the function Timer1_Timer() is automatically run?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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.
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 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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now