ms-access - 在 MS Access 中运行一系列查询时如何在状态栏上显示进度

标签 ms-access vba ms-access-2010 statusbar

我在 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/

相关文章:

c# - Dapper & MS Access - 读取有效,写入无效

string - 对字符串进行排序

database - 强制主键中的约束在 MS Access 中具有 "@"

mysql - 从 SQL 导出的 View 更新 MS Access 中的表

java - 如何使用数据库中的数据更新(刷新)JTable

mysql - ms-access 无法删除

sql - 如何在 MakeTable 查询 - Access 2000 中添加虚拟行?

excel - 仅将可见工作表中的可见单元格复制到新工作簿中,excel 2007 VBA

VBA 加密方式

excel - 将特定列从 Excel 传输到 Access Table 中的特定列