在 MS Access 中运行一系列查询时如何在状态栏上显示进度
How to show progress on status bar when running a sequence of queries in MS Access
我在 Windows 7 的 MS Access 2010 中有一个宏,它运行一系列非常慢的 Make Table 和 Update 查询。我希望它在状态栏上显示它正在运行的查询,因为通常的消息 "Run query" 没有给出查询名称。
我写了下面的VBA:
Function RunQueryAndReportStatusWithMsgBox(QueryName As String)
Dim RetVal As Variant
On Error GoTo ErrHandler
PutStatusBarBack
MsgBox "About to run query"
Application.Echo False, "Executing " & QueryName & " ..."
DoCmd.OpenQuery QueryName, acViewNormal, acEdit
On Error GoTo 0
Exit Function
ErrHandler:
Select Case Err
Case 2501: ' OpenQuery cancelled by the user pressing escape
MsgBox "The OpenQuery action for query " & QueryName & " was cancelled by the user."
Case Else: ' Another error has occurred.
' Display the error number and the error text.
MsgBox "Error # " & Err & " : " & Error(Err)
End Select
' Put status bar back to normal.
PutStatusBarBack
End Function
Function PutStatusBarBack()
Dim RetVal As Variant
On Error GoTo ErrHandler
' Put status bar back to normal.
RetVal = SysCmd(5) ' not sure if I need this.
Application.Echo True, ""
On Error GoTo 0
Exit Function
ErrHandler:
' Display the error number and the error text.
MsgBox "Error # " & Err & " : " & Error(Err)
' Put status bar back to normal.
RetVal = SysCmd(5) ' not sure if I need this.
Application.Echo True, ""
End Function
我写了一个宏来调用 RunQueryAndReportStatusWithMsgBox
,每个查询作为参数依次调用,然后我在宏的末尾调用 PutStatusBarBack
。我在开始时关闭警告,在结束时关闭警告。这真的很好用 - 就像我想要的那样。
但是,我不想在每次查询开始时都在消息框中按“确定”。如果我注释掉 MsgBox
语句,它就不再起作用了。结果是可变的。有时它会在状态栏中显示一些内容,有时则不会。当我刚才运行它时,我只收到 "Ready" 消息,但有时我会收到一些但不是所有查询所需的消息。
我试过使用 RefreshDatabaseWindow
而不是 MsgBox
,但这没有任何区别。
我不确定这就是您要找的东西?也许:
Dim statusText As String
Dim statusPercent As Integer
statusText = "Yada yada..."
statusPercent = 100 / 500 * 100
Application.StatusBar = "Progress: " & statusText & "(" & Cstr(statusPercent) & " %)" 'Progress: Yada yada... (20 %)
即每次您希望更改 Application.StatusBar
中的分配。
对应@Zajonc 对 Hauns TM 回答的评论。
它发生了,因为这行:
RetVal = SysCmd(5)
这意味着:刷新状态栏。
有关 MS Access 状态栏的更多信息:ACC: How to Change the Status Bar Text Using SysCmd()
所以,在第一个程序成功之前,不要刷新状态栏;)
For i = 1 to 10
SysCmd(4, "Running query " i & " of " & 10)
'your code here...
RunQueryAndReportStatusWithMsgBox(...)
Next
'here you should refresh status bar ;)
干杯,
马切
感谢 HansUp 帮助我回答了我后来发布的类似问题 (),我现在可以自己回答这个问题了。
要使代码在不调用 MsgBox 的情况下工作,您需要在调用 Application.Echo 之前放置两行:
RetVal = SysCmd(4, "Executing " & QueryName & " ...")
DoEvents
现在这正是我想要的。
刚才偶然发现了这个,所以它很可能太少,太晚了,但请确保在每次迭代期间,在您更改状态栏后调用 DoEvents。这告诉您的过程 return 控制应用程序并 Windows 一秒钟,这就是它更改状态栏文本的方式。这也是您如何防止 Access 查看 Windows 就像它没有响应一样。
我在 Windows 7 的 MS Access 2010 中有一个宏,它运行一系列非常慢的 Make Table 和 Update 查询。我希望它在状态栏上显示它正在运行的查询,因为通常的消息 "Run query" 没有给出查询名称。
我写了下面的VBA:
Function RunQueryAndReportStatusWithMsgBox(QueryName As String)
Dim RetVal As Variant
On Error GoTo ErrHandler
PutStatusBarBack
MsgBox "About to run query"
Application.Echo False, "Executing " & QueryName & " ..."
DoCmd.OpenQuery QueryName, acViewNormal, acEdit
On Error GoTo 0
Exit Function
ErrHandler:
Select Case Err
Case 2501: ' OpenQuery cancelled by the user pressing escape
MsgBox "The OpenQuery action for query " & QueryName & " was cancelled by the user."
Case Else: ' Another error has occurred.
' Display the error number and the error text.
MsgBox "Error # " & Err & " : " & Error(Err)
End Select
' Put status bar back to normal.
PutStatusBarBack
End Function
Function PutStatusBarBack()
Dim RetVal As Variant
On Error GoTo ErrHandler
' Put status bar back to normal.
RetVal = SysCmd(5) ' not sure if I need this.
Application.Echo True, ""
On Error GoTo 0
Exit Function
ErrHandler:
' Display the error number and the error text.
MsgBox "Error # " & Err & " : " & Error(Err)
' Put status bar back to normal.
RetVal = SysCmd(5) ' not sure if I need this.
Application.Echo True, ""
End Function
我写了一个宏来调用 RunQueryAndReportStatusWithMsgBox
,每个查询作为参数依次调用,然后我在宏的末尾调用 PutStatusBarBack
。我在开始时关闭警告,在结束时关闭警告。这真的很好用 - 就像我想要的那样。
但是,我不想在每次查询开始时都在消息框中按“确定”。如果我注释掉 MsgBox
语句,它就不再起作用了。结果是可变的。有时它会在状态栏中显示一些内容,有时则不会。当我刚才运行它时,我只收到 "Ready" 消息,但有时我会收到一些但不是所有查询所需的消息。
我试过使用 RefreshDatabaseWindow
而不是 MsgBox
,但这没有任何区别。
我不确定这就是您要找的东西?也许:
Dim statusText As String
Dim statusPercent As Integer
statusText = "Yada yada..."
statusPercent = 100 / 500 * 100
Application.StatusBar = "Progress: " & statusText & "(" & Cstr(statusPercent) & " %)" 'Progress: Yada yada... (20 %)
即每次您希望更改 Application.StatusBar
中的分配。
对应@Zajonc 对 Hauns TM 回答的评论。
它发生了,因为这行:
RetVal = SysCmd(5)
这意味着:刷新状态栏。
有关 MS Access 状态栏的更多信息:ACC: How to Change the Status Bar Text Using SysCmd()
所以,在第一个程序成功之前,不要刷新状态栏;)
For i = 1 to 10
SysCmd(4, "Running query " i & " of " & 10)
'your code here...
RunQueryAndReportStatusWithMsgBox(...)
Next
'here you should refresh status bar ;)
干杯,
马切
感谢 HansUp 帮助我回答了我后来发布的类似问题 (
要使代码在不调用 MsgBox 的情况下工作,您需要在调用 Application.Echo 之前放置两行:
RetVal = SysCmd(4, "Executing " & QueryName & " ...")
DoEvents
现在这正是我想要的。
刚才偶然发现了这个,所以它很可能太少,太晚了,但请确保在每次迭代期间,在您更改状态栏后调用 DoEvents。这告诉您的过程 return 控制应用程序并 Windows 一秒钟,这就是它更改状态栏文本的方式。这也是您如何防止 Access 查看 Windows 就像它没有响应一样。