TIME LOOP CODE

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've got this code to measure the amount of time a function runs. The code
works fine, but what I'd like to do (if possible), is supply the name of the
function as an argument, so I can use this one function to measure process
times for several different functions.

Somehow, I need my Call statement to "read" the argument supplied, so it can
interactively call the appropriate function to measure. How do I do this?

Function TimeLoop()

Dim sngStart As Single
Dim sngEnd As Single
Dim lngLoop As Long
Dim Msg As String
Dim Ans As Integer

sngStart = Timer

Call macInvUpdate

sngEnd = Timer

'Creates a message box
Msg = "You have successfully refreshed the Inventory table!"
Msg = Msg & vbNewLine & vbNewLine
Msg = Msg & "Process Time: " & Format$((sngEnd - sngStart) / 60, "0.0")
& " minutes"
Ans = MsgBox(Msg, vbInformation, "Refresh Status")


End Function
 
Kirk,

Unless I am missing something, this does not need to be a function because
you don't return anything from it. Unless you want to return a value, make
it a sub. Here is your code modified with comments:

Sub TimeLoop(strSubToCall as string) - Made it a sub, and pass a value
the value is
the name of the sub to
call
Dim sngStart As Single
Dim sngEnd As Single
Dim lngLoop As Long
Dim Msg As String
Dim Ans As Integer

sngStart = Timer

Call Eval(strSubToCall) - the Eval funtion converts the string passed
from the
caller

sngEnd = Timer

'Creates a message box
Msg = "You have successfully refreshed the Inventory table!"
Msg = Msg & vbNewLine & vbNewLine
Msg = Msg & "Process Time: " & Format$((sngEnd - sngStart) / 60, "0.0")
& " minutes"
Ans = MsgBox(Msg, vbInformation, "Refresh Status")


End Sub
 
[...]
Call Eval(strSubToCall) - the Eval funtion converts the string
passed from the
caller

You can only use Eval if strSubToCall is a Function, not a Sub, and you
have to include parentheseson the end of it if you want it to be
recognized. But you an use Application.Run for either a Function or a
Sub, without that requirement:

Application.Run strSubToCall
 
Dirk,

I'm using the Application.Run strSubToCall statement, and I'm getting a
"Microsoft
Access can't find the procedure" error.

I'm running the function in the Immediate window as such:

?TimeLoop(InvUpdate)

InvUpdate is a function (actually a converted macro) that simply performs a
series of delete, append and update processes to a table. Both functions
(TimeLoop and InvUpdate) are located in Module 1.

Any ideas?


Dirk Goldgar said:
[...]
Call Eval(strSubToCall) - the Eval funtion converts the string
passed from the
caller

You can only use Eval if strSubToCall is a Function, not a Sub, and you
have to include parentheseson the end of it if you want it to be
recognized. But you an use Application.Run for either a Function or a
Sub, without that requirement:

Application.Run strSubToCall

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Kirk P. said:
Dirk,

I'm using the Application.Run strSubToCall statement, and I'm getting
a "Microsoft
Access can't find the procedure" error.

I'm running the function in the Immediate window as such:

?TimeLoop(InvUpdate)

InvUpdate is a function (actually a converted macro) that simply
performs a series of delete, append and update processes to a table.
Both functions (TimeLoop and InvUpdate) are located in Module 1.

Any ideas?

So "InvUpdate" is the name of the procedure that you want the function
"TimeLoop" to call via Application.Run? You have to pass the name as a
string:

?TimeLoop("InvUpdate")

Otherwise, you'd be calling InvUpdate() and passing its return value to
TimeLoop().
 
Yep, that did it. Thanks for the help!

Dirk Goldgar said:
So "InvUpdate" is the name of the procedure that you want the function
"TimeLoop" to call via Application.Run? You have to pass the name as a
string:

?TimeLoop("InvUpdate")

Otherwise, you'd be calling InvUpdate() and passing its return value to
TimeLoop().

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Back
Top