?
Solved

Visual Basic Timers

Posted on 2003-03-02
9
Medium Priority
?
388 Views
Last Modified: 2013-11-25
I hate the timer in Visual Basic. Its limited by the size of an integer so you can't get anything above about one minute. Does anyone know of any free timers available or any other way to get a timer to go for at least 5 minutes?

     -Vinay
0
Comment
Question by:wertyk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 1

Accepted Solution

by:
Smashmad earned 100 total points
ID: 8054513
Hi, you can use Windows APIs:

Declare Function SetTimer Lib "user32.dll" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long

Declare Function KillTimer Lib "user32.dll" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long


'PUT THIS IN A .BAS MODULE

Public Sub TimerProc (ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, _


'HERE THE CODE YOU WANT TO EXECUTE EVERY X TIME


End sub


'PUT THIS CODE IN FORM1

Private Sub Form_Load()
     Dim retval As Long  ' return value
        Dim elapsed as Long
        elapsed = 300000

     retval = SetTimer(Form1.hWnd, 1, elapsed, AddressOf TimerProc)
End Sub

Private Sub Form_Unload(Cancel As Integer)
     Dim retval As Long  ' return value

     retval = KillTimer(Form1.hWnd, 1)
End Sub
0
 
LVL 1

Expert Comment

by:Smashmad
ID: 8054543
SORRY, PUT THIS IN THE .bas MODULE

Public Sub TimerProc (ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)


'The code you want...
'for example: form1.label1 = "blabla"

End Function
0
 
LVL 1

Expert Comment

by:Smashmad
ID: 8054571
this 2 declarations must be in the module too:

Declare Function SetTimer Lib "user32.dll" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long

Declare Function KillTimer Lib "user32.dll" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long

0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 3

Expert Comment

by:DeAn
ID: 8054672
here's a version without api

Dim StartTime As Date
Dim BigInterval As String

Private Sub Command1_Click()
    Timer1.Interval = 1000
    Timer1.Enabled = True
    StartTime = Now
    Me.Caption = StartTime
End Sub

Private Sub Form_Load()
    BigInterval = "00:00:10"
End Sub

Private Sub Timer1_Timer()
    If Now = StartTime + CDate(BigInterval) Then
        Timer1.Enabled = False
        MsgBox Now
    End If
End Sub
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8054675
for testing i had it set for 10 seconds, change BigInterval in Form_Load for any time up to a day.

' 5 minute interval
BigInterval = "00:05:00"
0
 
LVL 1

Expert Comment

by:gbaughma
ID: 8055004
... how about....

intMinutes = 0
intMinutesToTime = 5
Timer1.Interval = 60000
Timer1.Enabled = True

Private Sub Timer1_Timer()
If intMinutes < intMinutesToTime Then
     intMinutes = intMinutes + 1
Else
     ' Your timer is up!
End If
End Sub


... in other words, just increment a counter each minute.
0
 
LVL 4

Author Comment

by:wertyk
ID: 8056307
hey smashmad, what is the maximum value of a timer that can use this method?
0
 
LVL 1

Expert Comment

by:Smashmad
ID: 8058490
With this method, you can use a long integer, so,its like 4294967296 ms.. more or less 71582 minutes... 1193 hours.
0
 
LVL 4

Author Comment

by:wertyk
ID: 8061694
Thanks Smashmad, this worked really well. My program has a slider to determine the interval that the user wants and previously i could not set it at more than one minute without problems, but now the whole thing works.

             -Vinay
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

800 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