Search code examples
ms-accessvbams-access-2010statusbar

How to show progress on status bar when running a sequence of queries in MS Access


I have a macro in MS Access 2010 in Windows 7 which runs a sequence of quite slow Make Table and Update queries. I want it to show on the status bar which query it is running, as the usual message "Run query" does not give the query name.

I have written the following 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

I have written a macro to call RunQueryAndReportStatusWithMsgBox with each query in turn as an argument, and then I call PutStatusBarBack at the end of the macro. I turn warnings off at the start and on at the end. This works really well - just as I want it to.

However, I don't want to be pressing OK on a message box every time a query starts. If I comment out the MsgBox statement, it doesn't work any more. The results are variable. Sometimes it shows something in the status bar, sometimes not. When I ran it just now, I just got the "Ready" message throughout but sometimes I have got the desired message appearing for some but not all of the queries.

I have tried using RefreshDatabaseWindow instead of MsgBox, but that doesn't make any difference.


Solution

  • Thanks to the help I received from HansUp in answering a similar question (How to show progress on status bar when running code (not queries)) which I posted afterwards, I can now answer this question myself.

    To make the code work without the call to MsgBox, you need to put two lines before the call to Application.Echo:

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

    This now does exactly what I want.