Solved

Running a Function every 5 minutes

Posted on 2001-09-17
8
172 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need VBS version of Excel date conversion 4 64
Problem to skip loop 6 54
Excel VBA, find a string in a column, update a cell 7 65
VB6 - Convert HH:MM into Decimal 8 51
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…

912 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

16 Experts available now in Live!

Get 1:1 Help Now