我在 Windows 7 的 MS Access 2010 中有一个宏,它运行一系列非常慢的 Make Table 和 Update 查询。我希望它在状态栏上显示它正在运行的查询,因为通常的消息“运行查询”不会给出查询名称。
我编写了以下 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
语句,它就不再起作用了。结果是可变的。有时它会在状态栏中显示一些内容,有时则不会。当我刚才运行它时,我始终收到“就绪”消息,但有时我会收到一些但不是所有查询所需的消息。
我试过使用 RefreshDatabaseWindow
而不是 MsgBox
,但这没有任何区别。
最佳答案
刚才偶然发现了这个,所以它很可能太少,太晚了,但请确保在每次迭代期间,在您更改状态栏后调用 DoEvents。这会告诉您的过程将控制权返回给应用程序和 Windows 一秒钟,这就是它更改状态栏文本的方式。这也是您如何防止 Access 像没有响应一样查看 Windows。
关于ms-access - 在 MS Access 中运行一系列查询时如何在状态栏上显示进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27765376/