vba 显示代码运行时剩余的时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/337103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:05:00  来源:igfitidea点击:

Display time left when code is running

vbams-accessaccess-vba

提问by tksy

I have a form with a few buttons which execute code when pressed like running validations on the database.

我有一个带有几个按钮的表单,它们在按下时执行代码,就像在数据库上运行验证一样。

Some code can run for a few minutes so is there any way to show the time remaining or a message to display the % of process completed?

有些代码可以运行几分钟,所以有什么方法可以显示剩余时间或显示进程完成百分比的消息吗?

Or pop out a message when code evaluation starts and the message should disappear once code running is completed?

或者在代码评估开始时弹出一条消息,一旦代码运行完成该消息应该消失?

采纳答案by BIBD

What you are probably looking for is a "Progress Bar".

您可能正在寻找的是“进度条”。

I've used the Microsoft ProgressBar control (you can find it under Insert->ActiveX Control), and it's not that hard to use. Just set the value of it to a percentage (as an integer, not a decimal).

我用过Microsoft ProgressBar 控件(您可以在Insert->ActiveX Control 下找到它),使用起来并不难。只需将它的值设置为百分比(作为整数,而不是小数)。

'foo, being the ProgressBar
me.foo = 70 '70%

There is some good info here on another method: http://www.granite.ab.ca/access/progressbar.htm

这里有一些关于另一种方法的好信息:http: //www.granite.ab.ca/access/progressbar.htm

回答by tksy

Finally to be simple i decided to use the method given here

最后简单起见,我决定使用这里给出的方法

http://oreilly.com/pub/h/3330#code

http://oreilly.com/pub/h/3330#code

回答by Mike Powell

In order to do this the "normal" way, you'd need to run your validation in another thread and have it report its progress back to the UI thread. However, I don't believe VBA supports any kind of multithreading.

为了以“正常”方式执行此操作,您需要在另一个线程中运行验证并将其进度报告回 UI 线程。但是,我不相信 VBA 支持任何类型的多线程。

If your validation routines involve a loop, or even just many separate discrete operations, you can try inserting a DoEventsstatement in between loop iterations (or operations), and then have your progress display updated periodically (say, in an Application_OnTimeevent handler).

如果您的验证例程涉及循环,甚至只是许多单独的离散操作,您可以尝试DoEvents在循环迭代(或操作)之间插入一条语句,然后定期更新您的进度显示(例如,在Application_OnTime事件处理程序中)。

回答by Jon Fournier

I usually have a form I name frmProgress or whatever, with a cancel button and a label for displaying a status message. Then embedded in the form code I have a boolean called bCancel, and when you hit the cancel button it simply sets bCancel as true.

我通常有一个名为 frmProgress 的表单,带有一个取消按钮和一个用于显示状态消息的标签。然后嵌入到表单代码中,我有一个名为 bCancel 的布尔值,当您点击取消按钮时,它只是将 bCancel 设置为 true。

Also in this code I have a routine called ShowPercDone( Idx , NumIdc ) where Idx is the step the code is on, and NumIdc is the number of steps the code will take (assuming each step takes the same amount of time). This works well when I'm running through a for loop, but basically any time I want to display a status update I just call the routine in the form with my message, which I should add runs the doevents command for me.

同样在这段代码中,我有一个名为 ShowPercDone( Idx , NumIdc ) 的例程,其中 Idx 是代码所在的步骤,而 NumIdc 是代码将执行的步骤数(假设每个步骤花费的时间相同)。当我运行 for 循环时,这很有效,但基本上任何时候我想显示状态更新时,我只需在带有我的消息的表单中调用例程,我应该添加它为我运行 doevents 命令。

So that's how the status form works. In the macro I run, I start out by just calling frmProgress.show (0) so that it lets you click the cancel button. Then in my loop when I update the status message I then check frmProgress.bCancel and if it's true I exit out of the macro.

这就是状态表的工作原理。在我运行的宏中,我首先调用 frmProgress.show (0) 以便您单击取消按钮。然后在我的循环中,当我更新状态消息时,我检查 frmProgress.bCancel,如果它是真的,我退出宏。

Hope that helps.

希望有帮助。