vba 在 Microsoft Access 中测量查询处理时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8631975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Measuring query processing time in Microsoft Access
提问by Emmanuel Asogwa
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.
我得到了用于测量 Access 数据库中查询时间的代码。每次我尝试运行它时,都会出现语法错误并MyTest()
突出显示行。
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
回答by paulsm4
Here's another alternative (old VB6/VBA - not VB.Net syntax).
这是另一种选择(旧的 VB6/VBA - 不是 VB.Net 语法)。
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.
编辑。
回答by Heinzi
In the two lines
在两行
Debug.Print ElapsedTime() & _
you are using the string concatenation character &
and the line continuation character _
at the end of the line, even though the statement does not continue on the next line. So, either
您&
在行_
尾使用字符串连接字符和行继续字符,即使语句不在下一行继续。所以,要么
continue the statement on the next line, e.g.
Debug.Print ElapsedTime() & _ " milliseconds" ' or whatever unit is returned by your ElapsedTime call
or
Remove the unneeded code:
Debug.Print ElapsedTime()
继续下一行的语句,例如
Debug.Print ElapsedTime() & _ " milliseconds" ' or whatever unit is returned by your ElapsedTime call
或者
删除不需要的代码:
Debug.Print ElapsedTime()
PS: Welcome to StackOverflow. Please get a good book on VBA development and read it. It's ok to ask basic questions here, but "please fix this code I found, because I don't know the basics of the language and don't have the time to learn it" type of questions are frowned upon and likely to be closed.
PS:欢迎使用 StackOverflow。请找一本关于 VBA 开发的好书并阅读它。在这里提出基本问题是可以的,但是“请修复我找到的这段代码,因为我不了解该语言的基础知识并且没有时间学习它”类型的问题不受欢迎并且可能会被关闭.