2015-01-04 34 views
1

我在Windows 7中的MS Access 2010中有一个宏,它运行一系列相当慢的Make Table和Update查询。我希望它显示在状态栏上查询它正在运行,因为通常的消息“运行查询”不会给出查询名称。如何在MS Access中运行一系列查询时在状态栏上显示进度

我已经写以下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,但这没有什么区别。

回答

2

得益于帮助我从HansUp那里收到了我后来发布的类似问题(How to show progress on status bar when running code (not queries)),现在我可以自己回答这个问题。

为了使代码工作,而不调用MSGBOX,你需要把两行调用Application.Echo前:

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

现在,这不正是我想要的。

0

我不确定这是你在找什么?也许:

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中的作业。

+0

感谢您的建议Hans TM。不幸的是Access不理解Application.StatusBar。我最初尝试了RetVal = SysCmd(4,“Executing”&QueryName&“...”),但状态栏被Access覆盖,并显示“运行查询”。 – Zajonc 2015-01-04 13:11:14

1

对应@ Zajonc对Hauns TM的评论回答。

它发生,因为OT这一行:

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 ;) 

干杯,
马切伊

+0

谢谢Maciej,但不幸的是这仍然不起作用。 SysCmd(4,...)被Access自己的消息“运行查询”覆盖。唯一的解决方法似乎是使用回声功能。我感觉我很亲密,因为它可以和MsgBox一起使用! – Zajonc 2015-01-08 13:45:26

+0

@Zajonc,我想说的是从** RunQueryAndReportStatusWithMsgBox *过程中删除** PutStatusBarBack **) – 2015-01-08 13:49:34

+0

谢谢Maciej。我拿到了我的原始代码(参见上面的问题),并在第一次调用PutStatusBarBack之前发表了一条评论(MsgBox调用也被注释掉了,正如我的问题所述)。评论PutStatusBarBack没有任何区别。大多数情况下,状态栏中除“准备好”之外都没有显示任何内容。它对你有用吗? – Zajonc 2015-01-08 15:42:25

3

刚刚遇到了这个问题,所以它很可能太少,太迟了,但要确保在每次迭代中,在更改状态栏后调用DoEvents。这告诉您的过程将控制返回到应用程序和Windows一秒,这是它如何更改状态栏文本。这也是你如何让Access不从Windows查看,因为它没有响应。