【问题标题】:How to show progress on status bar when running a sequence of queries in MS Access在 MS Access 中运行一系列查询时如何在状态栏上显示进度
【发布时间】:2015-01-04 12:14:48
【问题描述】:

我在 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。我在开始和结束时关闭警告。这真的很有效 - 正如我想要的那样。

但是,我不想在每次查询开始时都在消息框上按 OK。如果我注释掉MsgBox 语句,它就不再起作用了。结果是可变的。有时它会在状态栏中显示某些内容,有时则不会。当我刚刚运行它时,我一直收到“就绪”消息,但有时我会收到一些但不是所有查询的所需消息。

我尝试使用RefreshDatabaseWindow 代替MsgBox,但这没有任何区别。

【问题讨论】:

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


    【解决方案1】:

    刚刚偶然发现这个,所以它很可能太少,太晚了,但请确保在每次迭代期间,在更改状态栏后调用 DoEvents。这会告诉您的过程将控制权返回给应用程序和 Windows 一秒钟,这就是它更改状态栏文本的方式。这也是防止 Access 像没有响应一样查看 Windows 的方法。

    【讨论】:

      【解决方案2】:

      感谢 HansUp 帮助我回答了我之后发布的类似问题 (How to show progress on status bar when running code (not queries)),现在我可以自己回答这个问题了。

      要使代码在不调用 MsgBox 的情况下工作,您需要在调用 Application.Echo 之前放置两行:

      RetVal = SysCmd(4, "Executing " & QueryName & " ...")
      DoEvents
      

      这正是我想要的。

      【讨论】:

        【解决方案3】:

        对应于@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 ;)
        

        干杯,
        马切耶

        【讨论】:

        • 谢谢 Maciej,但不幸的是这仍然不起作用。 SysCmd(4,...) 被 Access 自己的消息“运行查询”覆盖。解决这个问题的唯一方法似乎是使用 Echo 功能。我觉得我很接近,因为它可以与 MsgBox 一起使用!
        • @Zajonc,我想说的是从 *RunQueryAndReportStatusWithMsgBox 过程中删除 PutStatusBarBack ;)
        • 感谢 Maciej。我使用了我的原始代码(参见上面的问题)并在第一次调用 PutStatusBarBack 之前添加了注释(如我的问题中所述,MsgBox 调用也被注释掉了)。注释掉 PutStatusBarBack 没有任何区别。大多数情况下,除了“就绪”之外,状态栏中没有显示任何内容。它对你有用吗?
        • 非常有趣的行为...不,我没有测试它。今天很晚(大约上午 00:00)。我明天再看看你的代码。
        【解决方案4】:

        我不确定这就是您要找的东西?也许:

        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 中的分配。

        【讨论】:

        • 感谢您的建议 Hans TM。不幸的是,Access 不理解 Application.StatusBar。我确实尝试过 RetVal = SysCmd(4, "Executing " & QueryName & " ...") 最初,但状态栏被 Access 说“运行查询”覆盖。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-02
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 2015-12-18
        • 1970-01-01
        相关资源
        最近更新 更多