Vba – Measuring query processing time in Microsoft Access

ms-accessms-access-2010vba

I got this code for measuring the time of a query in Access database. Every time I try to run it, I get syntax error and MyTest() line is highlighted.

Option Compare Database

Option Explicit

Private Declare Function timeGetTime _
Lib "winmm.dll" () As Long
Private mlngStartTime As Long

Private Function ElapsedTime() As Long
ElapsedTime = timeGetTime() - mlngStartTime
End Function

Private Sub StartTime()
mlngStartTime = timeGetTime()
End Sub

Public Function MyTest()

Call StartTime
DoCmd.OpenQuery "Query1"
DoCmd.GoToRecord acDataQuery, "Query1", acLast

Debug.Print ElapsedTime() & _

Call StartTime
DoCmd.OpenQuery "Query2"
DoCmd.GoToRecord acDataQuery, "Query2", acLast

Debug.Print ElapsedTime() & _
End Function

Best Answer

Here's another alternative (old VB6/VBA - not VB.Net syntax).

KEY SUGGESTION: the "_" characters are "continuation lines". I honestly don't think you want them in most of the places you're using them.

IMHO...

Option Explicit

Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private startTime, endTime As Long

Private Function elapsedTime(t1, t2 As Long) As Long
  elapsedTime = t2 - t1
End Function

Public Function MyTest()

  startTime = Now
  ' << do stuff >>
  endTime = Now
  MsgBox "Elapsed time=" & elapsedTime(startTime, endTime)

End Function

Private Sub Command1_Click()
  Call MyTest
End Sub

edited.

Related Topic