UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> how to time how long a query takes    
 
   
LindseyM
post Jun 4 2005, 08:46 AM
Post #1

UtterAccess Guru
Posts: 524
From: North West, UK



Hi everyone

What I'm doing is running some queries when a command button is clicked and a msgbox is displayed to say that the queries are about to run and then another one is displayed when they have finished. What I want to do is in the 2nd msgbox, display how long it took for the all the queries to run - here is my code:
CODE
Private Sub cmd_run_queries_Click()

MsgBox "Please wait..... the queries you are running may take several minutes", vbOKOnly, "Running...."
    
    DoCmd.SetWarnings False
    DoCmd.Hourglass True
    'these queries need to be run for the Evaluation Summary Report and the Average Quality Report
    CurrentDb.Execute "qry_DE_evaluation_summary" ' for the evaluation summary report
    CurrentDb.Execute "qry_MT_evaluation_summary"
    CurrentDb.Execute "qry_DE_agents"
    CurrentDb.Execute "qry_agents"
    CurrentDb.Execute "qry_DE_comparison"
    CurrentDb.Execute "qry_MT_comparison"
    
    
    DoCmd.Hourglass False
    DoCmd.SetWarnings True
    
    MsgBox "Query run complete"

End Sub


Anyone got any ideas on how I would do this?

Thanks in advance for any help......(IMG:http://www.utteraccess.com/forum/style_emoticons/default/laugh.gif)

Linds
Go to the top of the page
 
+
strive4peace
post Jun 4 2005, 10:05 AM
Post #2

UtterAccess VIP
Posts: 20,211
From: Colorado



rewrite your code to the following:
CODE
Private Sub cmd_run_queries_Click()
  
  StartTime "Evaluation Summary Report and the Average Quality Report"

   MsgBox "Please wait..... the queries you are running may take several minutes", vbOKOnly, "Running...."      
  
   'these queries need to be run for the Evaluation Summary Report and the Average Quality Report    

   rsql, "qry_DE_evaluation_summary", "for the evaluation summary report"

   rsql "qry_MT_evaluation_summary" , "MT_evaluation_summary"

   rsql "qry_DE_agents" , "DE_agents"

   rsql "qry_agents" , "agents"

   rsql "qry_DE_comparison", "DE_comparison"

   rsql "qry_MT_comparison", "MT_comparison"  

   ReportElapsedTime  "Query run complete"

End Sub


then

on timing, instead of using currentdb.execute strSQL ...

1. put this code in a general module
2. use
rSql s, "brief description"

then, you can see the timing on each SQL statement (if it executed normally) in the debug window -- you will always see the SQL ...

Don't forget to clear the status bar when your program is done since this sets the status bar with each sql statement... or you can use the "overall timer" posted below, which clears the statusbar

CODE
Sub rSql(pSQL, Optional pMsg, Optional UseDocmd As Boolean)

   'written by Crystal
   'strive4peace2004@yahoo.ca

   On Error GoTo rSQL_Error
  
   Dim mTime As Date
   mTime = Now()
  
   If Not IsMissing(pMsg) Then
      Debug.Print "---------- " & pMsg
      SysCmd acSysCmdSetStatus, pMsg & "..."
   End If

   Debug.Print pSQL

   If Not  IsMissing(UseDocmd) Then
      'when aggregate functions are used, sometimes it is necessary to use this method
      If UseDocmd Then
         DoCmd.Echo False
         DoCmd.SetWarnings False
         DoCmd.RunSQL pSQL
         DoCmd.Echo True
         DoCmd.SetWarnings True
      Else
         CurrentDb.Execute pSQL
      End If
   Else
         CurrentDb.Execute pSQL
   End If

   Debug.Print " --- " & Format((Now() - mTime) * 24 * 60 * 60, "#,##0.#") & " seconds" & " --- "
   Exit Sub
  
rSQL_Error:
   DoCmd.Echo True
   DoCmd.SetWarnings True
      
End Sub



sometimes I throw a
DoEvents
into the rSQL code when I am having troubles... and sometimes I also set up a table to capture the SQL statements and the times.

and then, here is the overall timer...

CODE
Option Explicit

'written by Crystal
'strive4peace2004@yahoo.ca
  
Dim gStartTime As Date

'and, if you want to number the lines
'dim gLineNumber As Integer


Sub StartTime(Optional pMsg)
   On Error Resume Next
   gStartTime = Now()
   DoCmd.Hourglass True
'   gLineNumber = 0
   If IsMissing(pMsg) Then Exit Sub
   Debug.Print "--- START-------------" & pMsg & " ----- " & CStr(gStartTime)
End Sub

Sub EndTime()
   Debug.Print "--- END-------------" & DateDiff("s", gStartTime, Now()) & " seconds"
   DoCmd.Hourglass False
   SysCmd acSysCmdClearStatus
End Sub
  
Sub ReportElapsedTime(Optional pMessage)

   On Error Resume Next
   Dim m As String, mEndTime As Date
   mEndTime = Now()
   DoCmd.Hourglass False

   'if you are numbering lines... increment counter and change debug.print to include it
   'gLineNumber = gLineNumber  + 1

   If IsMissing(pMessage) Then
      m = ""
   Else
      m = pMessage & ": " & vbCrLf & vbCrLf
      Debug.Print "-------------" & pMessage & " ----- "
   End If
   m = m & "Start Time: " & Format(gStartTime, "hh:nn:ss") & vbCrLf _
      & "End Time: " & Format(mEndTime, "hh:nn:ss") & vbCrLf & vbCrLf _
      & "Elapsed: " & Format((mEndTime - gStartTime) * 24 * 60, "0.##") & " minutes"
   SysCmd acSysCmdClearStatus
   MsgBox m, , "Time to execute         "
      '& CStr(Nz(pFirstSQL, -1))
  
End Sub



StartTime goes, of course, at the top of the routine

EndTime OR ReportElapsedTime when it is all over...

When your routine is done, open the DEBUG window
CTRL-G from a module window

and look at what was written -- you will have an overall time as well as a time for each step

If any of your routines use aggregate domain functions (dSum, dCount, etc) -- use the optional 3rd argument of TRUE for "rsql"

ie:
rsql, "queryname", "message", TRUE
Go to the top of the page
 
+
LindseyM
post Jun 4 2005, 10:26 AM
Post #3

UtterAccess Guru
Posts: 524
From: North West, UK



Thank you Crystal, being a newbie, its going to take me some time I think to get my head around all of the above - I had a feeling it wasn't going to simple!! lol

I will get back to you once i have implemented this in my code and let you know how I get on.

Thanks so much again

Linds
Go to the top of the page
 
+
strive4peace
post Jun 4 2005, 11:00 AM
Post #4

UtterAccess VIP
Posts: 20,211
From: Colorado



au contraire! It IS simple...

1. replace your routine with the code I gave you

2. create a general module
from the database window, click Modules tab, then NEW
you will be on a blank module sheet

3. copy the Overall Timer code to the module sheet (last codeblock)

4. copy the rsql code and paste below the overall timer code


that gets the code in...

Whenever you write or paste code, your should ALWAYS compile it before you attempt to run it.

from the menu: Debug, Compile

fix any errors on the yellow highlighted lines

keep compiling until nothing happens (this is good!)
Go to the top of the page
 
+
DougY
post Jun 4 2005, 01:54 PM
Post #5

Utterly Abby-Normal
Posts: 9,756
From: Seattle, WA [USA]



PMFJI,

Crystal's code is solid (as always) and although it may take a few minutes to implement, it will provide you with great information. However, if all you want is the total time that it took to execute the queries, the following code will do it:

CODE
    Dim dtmStart As Date    'start time
    Dim dtmEnd As Date        'end time
    Dim varMin As Variant    'calculate minutes
    Dim varSec as Variant    'calculate seconds
    
    dtmStart = Now()
    [color="blue"]...Your Code...    [/color]
    dtmEnd = Now()
        
    varSec = DateDiff("S", dtmStart, dtmEnd)
    varMin = DateDiff("N", dtmStart, dtmEnd)
        
    Msgbox "It took " & varMin & " Minutes and "_
     & varSec & "Seconds to run the queries"


HTH
Go to the top of the page
 
+
R. Hicks
post Jun 4 2005, 02:04 PM
Post #6

UA Forum Administrator
Posts: 38,073
From: Birmingham, Alabama USA



Don't think that will return the correct results ...

To get the elapsed minutes .. you would used:
CODE
DateDiff("s", dtmStart, dtmEnd) \ 60

To get the remaining seconds .. you would use:
CODE
DateDiff("s", dtmStart, dtmEnd) Mod 60

RDH
Go to the top of the page
 
+
DougY
post Jun 4 2005, 02:10 PM
Post #7

Utterly Abby-Normal
Posts: 9,756
From: Seattle, WA [USA]



(IMG:http://www.utteraccess.com/forum/style_emoticons/default/blush.gif)
When I tested it, the action I used took less then a minute...
Didn't think about the 61+ seconds...

Thanks for catching it Ricky (IMG:http://www.utteraccess.com/forum/style_emoticons/default/frown.gif)
Go to the top of the page
 
+
R. Hicks
post Jun 4 2005, 02:12 PM
Post #8

UA Forum Administrator
Posts: 38,073
From: Birmingham, Alabama USA



No problem ... (IMG:http://www.utteraccess.com/forum/style_emoticons/default/wink.gif)

RDH
Go to the top of the page
 
+
HiTechCoach
post Jun 4 2005, 06:09 PM
Post #9

UtterAccess VIP
Posts: 18,396
From: Oklahoma City, Oklahoma



Crystal,

Great code for testing.

Thanks for sharing it.

(IMG:http://www.utteraccess.com/forum/style_emoticons/default/uarulez2.gif)
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 25th May 2013 - 12:38 AM