UNKNOWN '************************************** 'Windows API/Global Declarations for :De ' bugTimer '************************************** Add a new class module to your project, and name it clsDebugTimer. Paste the following code into it: ' METHODS: ' ' Begin(nTimerIndex, nTimerDescription) ' - Starts/resets a new timer. Both para ' meters are optional. ' ' nTimerIndex should be a number from 0 ' to 9 to specify ' which timer is to be used. Omitting th ' is param is the same ' as passing a zero as this parameter. ' ' nTimerDescription is a description whi ' ch can be anything you ' like, but should probably describe wha ' t it is you are timing. ' Omitting this param will set the descr ' iption to "Timer 1" (or ' whatever time index you are using inst ' ead of 1) ' ' ' ShowElapsed(nOutputType, nTimerIndex) ' -Displays the elasped time for the tim ' er specified in nTimerIndex ' since the Begin method was called. Bot ' h parameters are optional. ' ' nOutputType should be either 1 or 2, a ' nd you can use the constants ' outImmediateWindow and outMsgBox, repe ' ctively. This param ' determines where the output will go- e ' ither the immediate window or ' a message box. The description will be ' displayed along with the ' elpased time. If this param is omitted ' , the output goes to the immediate ' window. ' ' nTimerIndex is used to specify which t ' imer you want to display the ' elapsed time for. (See the description ' in the Begin method, above). ' If omitted, timer number 0 (zero) is u ' sed. ' ' 'PROPERTIES: ' 'Elapsed(nTimerIndex) ' -Returns the number of seconds that ha ' ve elapsed since the Begin ' method was called for the specified ti ' mer. If nTimerIndex is omitted, ' timer 0 (zero) is assumed. ' ' Option Explicit Public Enum OutputTypes outImmediateWindow = 1 outMsgBox = 2 End Enum Dim nBegin(10) As Single Dim sDesc(10) As String Public Sub Begin(Optional nTimerIndex As Integer, Optional sTimerDescription As String) If (nTimerIndex < 0 Or nTimerIndex > 9) Then Exit Sub If sTimerDescription = "" Then sTimerDescription = "Timer " & Trim(Str(nTimerIndex)) nBegin(nTimerIndex) = Timer sDesc(nTimerIndex) = sTimerDescription End Sub Public Property Get Elapsed(Optional nTimerIndex As Integer) As Single If (nTimerIndex < 0 Or nTimerIndex > 9) Then Exit Property Elapsed = Val(Format(Timer - nBegin(nTimerIndex), "####.##")) End Property Public Sub ShowElapsed(Optional nOutputType As OutputTypes, Optional nTimerIndex As Integer) If nOutputType = 0 Then nOutputType = outImmediateWindow If nOutputType < outImmediateWindow Or nOutputType > outMsgBox Then Exit Sub If nOutputType = outImmediateWindow Then Debug.Print sDesc(nTimerIndex) & ": " & Elapsed(nTimerIndex) & " seconds" Exit Sub End If If nOutputType = outMsgBox Then MsgBox sDesc(nTimerIndex) & ": " & Elapsed(nTimerIndex) & " seconds", vbOKOnly, "Debug Timer" Exit Sub End If End Sub '************************************** ' Name: DebugTimer ' Description:Have you ever been asked: ' Which part of the routine is taking so l ' ong? or did you ever wonder what functio ' n was bogging your app down, or did you ' ever just want to time a particular stat ' ement or function? Welcome to DebugTimer ' . It's not a resource hog and uses no ac ' tive-x controls... just the built-in Tim ' er function in VB. This is a very easily ' implemented class module that allows you ' to time any line(s) of code or functions ' or whatever. You can even use multiple t ' imers or nest them. I wrote this to dete ' rmine the length of time it took to perf ' orm various stored procedures, and it wo ' rked great. If you have a similar need, I'm sure this will do the trick. ' By: Matthew Heydman ' ' ' Inputs:None ' ' Returns:None ' 'Assumes:None ' 'Side Effects:None 'This code is copyrighted and has limite ' d warranties. 'Please see http://www.Planet-Source-Cod ' e.com/xq/ASP/txtCodeId.754/lngWId.1/qx/v ' b/scripts/ShowCode.htm 'for details. '************************************** 'Add a new Form to your project, and add ' 3 command buttons to the 'form (named Command1, Command2, and Com ' mand3). Then just 'paste the following code into the form: ' Option Explicit Dim i As Integer Dim dbg As New clsDebugTimer Private Sub Command1_Click() Me.MousePointer = vbHourglass 'EXAMPLE 1 - VERY BASIC USAGE ' Start the timer dbg.Begin 'Do something that will take a little ti ' me For i = 0 To 25000: DoEvents: Next 'By default, calling the ShowElapsed met ' hod 'will display the elapsed time in the im ' mediate window dbg.ShowElapsed Me.MousePointer = vbDefault End Sub Private Sub Command2_Click() Me.MousePointer = vbHourglass 'EXAMPLE 2 - USING THE PARAMETERS 'Start the timer, this time passing a 'timer index and a description dbg.Begin 0, "Loop from 0 to 25000" 'Do something that takes time For i = 0 To 25000: DoEvents: Next 'Display the elapsed time for timer inde ' x 0 in a message box dbg.ShowElapsed outMsgBox, 0 Me.MousePointer = vbDefault End Sub Private Sub Command3_Click() Me.MousePointer = vbHourglass 'EXAMPLE 3 - USING MULTIPLE TIMERS 'Start the first timer- we'll use an ind ' ex of 1 'timer index and a description dbg.Begin 1, "Total Time" 'Start a second timer- (index 2) 'timer index and a description dbg.Begin 2, "Count from 0 to 25000" 'Do something that takes time For i = 0 To 25000: DoEvents: Next 'Display the elapsed time for the second ' timer dbg.ShowElapsed outImmediateWindow, 2 'perform another loop like the one we ju ' st did above dbg.Begin 2, "Count from 0 to 24999" 'Do something that takes time For i = 0 To 24999: DoEvents: Next 'Display the elapsed time for the second ' timer dbg.ShowElapsed outImmediateWindow, 2 'Now display the elapsed time for the fi ' rst timer dbg.ShowElapsed outImmediateWindow, 1 Me.MousePointer = vbDefault End Sub