Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count time to run all reports 1

Status
Not open for further replies.

mbowler9

IS-IT--Management
Sep 8, 2003
105
US
I have a form that allows me to hit cmdRunReports and it will run 50 reports over a few minutes. I have a text box called txtTime that I would like to show the elapsed time (count up from 0 in hh:nn:ss format) since cmdReports was hit.

Any suggestions?

I am new to these forms so please be patient.

Thanks
 
Keeping a running time can be accomplished by using the Form's Timer event. But since you don't want the Timer event to start until begin your process, I suggest that you disable the timer in the Form_Load event.
Code:
Private Sub Form_Load()
   Me.TimerInterval = 0
End Sub
When you want the timer to begin, set the timer interval, which is the number of milliseconds between Timer Events. For a one second interval, you would set the timer interval to 1000. At the completion of the process, you need to disable the timer by setting the interval back to 0.
Code:
Private Sub cmdBeginProcess_Click()
   txtElapTime.Tag = Time()
   Me.TimerInterval = 1000
      <Build your Reports>
   Me.TimerInterval = 0
   txtElapTime.Tag = vbNullString
End Sub
Then inside the timer event, you would have the following code
Code:
Private Sub Form_Timer()
   txtElapTime = DateDiff(&quot;s&quot;, txtElapTime.Tag, Time())
   DoEvents
End Sub
You may be wondering why the .Tag property is initialized to Time() at the start of the interval and why in the Timer Event the DateDiff function is being used, rather than initializing to zero and simply incrementing by one, since the interval is one second. The answer lies in how events are processed, and what exactly you wish to capture, and the fact that we don't work in an optimal world and Windows is not a real-time OS. Every second, a Timer event will be placed on the Windows message queue. However, there will a time delay before Windows actually processes that message, and delivers it to the Application message queue. Another time delay will occur before the application will handle the event. The result is that by the time the event is handled, more than one second will have passed. Depending on how processor intensive your code is, and other processes running concurrently with your application, it may be almost immediate that the event is handled, or it may actually be a second or two before the message is actually handled by your application. If you simply increment the txtElapTime value, you will be counting the number of times that you application handled a Timer event, and not the elapsed time. Consequently, you capture the actual time the processed started, and display the elapsed time between the start time and the time that the current Timer event is being handled. You can improve the granularity of the event by using DoEvents inside of your process at appropriate times, which provides your application opportunities to catch up on the pending messages, such as the pending Timer events. You can also reduce your own time interval between events. Unfortunately, there are a few other processes running that may not be able to control to insure that you will be able to process the Timer events in precisely one second intervals.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I have probably done something wrong along the way here, but for some reason it is acting a little wierd. The text box remains at &quot;0&quot; which is what I initialize it to in the load_event. When I hit Ctrl+Break to stop the process (like I said it takes several minutes) the text box will pick up at where it should be. So if I stop the processes 55 seconds in, the text box which was &quot;0&quot; the whole time, will then start counting at 55.

Please take a look at my code. I understand that the reason it keeps counting is that I do not set the TimerInterval back to 0 (I actually do it at the end of the function &quot;make_directory&quot;), but I don't understand why it doesn't show until after I break.


Option Compare Database
Option Explicit


'initializes text fields
'initializes variables
Private Sub Form_Load()

Forms!frmAutoRun!txtStatus = 0
Forms!frmAutoRun!txtTime = 0
Me.TimerInterval = 0

End Sub

'this actually calls the functions that create
'directories and export the reports
Private Sub cmdRunReports_Click()

txtTime.Tag = Time()
Me.TimerInterval = 1000

Call make_directory

txtTime.Tag = vbNullString

End Sub


Private Sub Form_Timer()

txtTime = DateDiff(&quot;s&quot;, txtTime.Tag, Time())
DoEvents

End Sub
 
As I said earlier, you need to place some DoEvents in your code to allow your application to &quot;catch up&quot; and process the pending events. The DoEvents need to be placed at various locations within your make_directory function.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thanks. It worked, but I couldn't get the timer to be fluid enough no matter how many DoEvents I added, so rather than displaying the real time, I just display the total elapsed time at the end.
 
It's awfully hard to do so because Windows is simply not a real-time system, and there are so many other things happening in the background.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top