I want to use the timer function to get latest information from database
if it's more than 1 minute then the function will get the latest data and update the text box.
Pls guide me how to do this.
Timer interval is in milliseconds. To have code execute 1 time each minute place a timer on your form, set the timer interval to 60,000, set the enabled property to true and place a call to your code in the timer tick event.
Depending on the amount of processing the Timer event must do, setting it for each minute can cause some really sluggish ( slow ) applications, be careful if that is the case
Best to use a ONE SECOND (or so) timer, and count the CLICKS
It counts UP for a minute...
Code:
Option Explicit
' Add a Timer control to your project. It will be Timer1
' It looks like a stop watch in the IDE.
Dim OldTime As Date
Dim newTime As Date
Dim diff As Date
Private Sub Form_Load()
OldTime = Time
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Static x As Long
Static zz$, ss$
x = x + 1
newTime = Time
diff = DateDiff("s", OldTime, newTime)
Form1.Caption = (diff \ 3600) & ":" & Format((diff \ 60 Mod 60), "00") & ":" & _
Format((diff - ((diff \ 60) * 60)), "00")
' Debug.Print (diff \ 3600) & ":" & Format((diff \ 60 Mod 60), "00") & ":" & _
Format((diff - ((diff \ 60) * 60)), "00")
If newTime = DateAdd("s", 360, OldTime) Then ' Add 360 seconds
Timer1.Enabled = False
' You time is UP! Do something!
Beep
End If
End Sub
Note to self: Reinstall VB6... Knew I forgot something...
I would disagree, True it will fire every minute but it is also firing 59 times in between and doing some processing each time. This is not a big deal but getting in a habit of writing such code will result in programs running slower than they should.
If you need it to fire every 5 to 10 minutes I would set the interval to 1 minute and use a counter to see how many minutes have elapsed using a static var with a counter as is the case in the example above with X though X is not used in the code just as a wasted process.
In your timer routine
Code:
Static x As Integer
x=x+1
If X> 5 then
X=0
'Do your processing
End IF
Last edited by DataMiser; June 9th, 2011 at 10:46 AM.
Bookmarks