简体   繁体   English

“请等待”弹出消息

[英]“Please Wait” popup message

I am trying to create a Please Wait message in Excel VBA. 我试图在Excel VBA中创建一个Please Wait消息。 This is to remind the user processing a SQL query that the process is still ongoing. 这是为了提醒用户处理SQL查询,该进程仍在进行中。 I would like to have this message displayed in a userform. 我想在userform中显示此消息。

Currently, the user connects to SQL DB, selects database and some other options, presses Go... and the form does not unload. 目前,用户连接到SQL DB,选择数据库和其他一些选项,按下Go ...并且表单不会卸载。 At the Go point I have another form which pops up with message Please Wait , but this form does not unload, ie the SQL execute command does not run. 在Go点我有另一个弹出消息Please Wait表单,但是这个表单没有卸载,即SQL执行命令没有运行。

My code for calling the Please Wait userform, including a label: 我调用Please Wait userform的代码,包括一个标签:

   Call WaitShowSQl.ExecuteCall KillWaitCall WaitShow
   SQl.Execute
   Call KillWait

In another code module I have: 在另一个代码模块中,我有:

   Sub WaitShow()
   Wait.Show
   End Sub

   Sub KillWait()
   Unload Wait
   End Sub

I have userform called wait with following code: 我有一个名为wait userform,代码如下:

Private Sub UserForm_Activate()
Call PleaseWait
End Sub 

Sub PleaseWait()
Wait.Label1.Caption = "Calculating...Please Wait!"
End Sub

Now when executing the code, the run-time does not move back through the module in order to execute the following: 现在,在执行代码时,运行时不会通过模块返回以执行以下操作:

SQl.ExecuteSQl.Execute

I can show the progress of the program with a status bar, but a userform popup message would be very useful for this program. 我可以使用状态栏显示程序的进度,但userform弹出消息对此程序非常有用。

The default behavior of the Form.Show method is to show the form as a modal dialog. Form.Show方法的默认行为是将表单显示为模式对话框。 What you are looking for is a modeless dialog box, so you need to specify that, like this: 你正在寻找的是一个无模式对话框,所以你需要指定,如下所示:

Sub WaitShow()
   Wait.Show vbModeLess
End Sub

Update: 更新:

If you can't user a modeless dialog, you have other options, as mentioned in a comment. 如果您无法使用无模式对话框,则还有其他选项,如注释中所述。 One of them is to let the wait userform do the SQL executing. 其中一个是让wait userform执行SQL。 This could be done like this: 这可以这样做:

In the wait form, first declare variables 在等待表单中,首先声明变量

Private mySomeParameter1 As String
Private mySomeReturnValue1 As String

'Input parameters to the form
Public Sub Init(ByVal some_parameter1 As String, ...)
... Initialize your SQL, NOT execute it
End Sub

'Output values from the from
Public Sub GetReturns(ByRef some_return_value1 As String, ...)
... set the output parameters...
End Sub

'Do the work
Private Sub UserForm_Activate()
  Call PleaseWait
  'Either call DoEvents or a manual Refresh to let the label display itself
  Call ExecutSQL    ' A function that executes the SQL
  Unload Me
End Sub

And in the main form: 并以主要形式:

Sub WaitShow()
   Wait.Init(the parameters...)
   Wait.Show
   Wait.GetReturns(the results...)
End Sub

Yet another option for you would be to skip the Wait form completely and instead show an "Waiting" Image on the mainform and hide it when the SQL processing is done. 另一个选择是完全跳过Wait表单,而是在mainform上显示“Waiting”图像,并在SQL处理完成后隐藏它。

Here's how to show a progress bar while your users are waiting for Excel to finish processing: 以下是在用户等待Excel完成处理时显示进度条的方法:

https://stackoverflow.com/a/10791349/138938 https://stackoverflow.com/a/10791349/138938

Excel进度条

In your case, you can guess a reasonable "average" time for your queries and have the progress bar update with percentages of that time, which will reassure your users that Excel is working and they don't need to panic. 在您的情况下,您可以猜测查询的合理“平均”时间,并使用该时间的百分比更新进度条,这将使您的用户放心Excel正在运行且他们不需要恐慌。

My solution is far less elegant and impressive than the others. 我的解决方案远没有其他人那么优雅和令人印象深刻。 FYI, my script is doing processing for a number of partners within a FOR...NEXT loop, and I want to update the screen with the partner currently being processed. 仅供参考,我的脚本正在为FOR ... NEXT循环中的许多合作伙伴进行处理,我想要与当前正在处理的合作伙伴一起更新屏幕。

I created a worksheet tab called "Messages". 我创建了一个名为“Messages”的工作表选项卡。 I expanded cell A1 as wide and tall as I could, and I picked a font that I liked a color scheme that I liked. 我尽可能宽大地扩展了单元格A1,我选择了一种我喜欢的颜色方案,我喜欢它。 My script starts by turning off ScreenUpdating. 我的脚本首先关闭ScreenUpdating。 I then 然后我

Sheets("Messages").Select
Cells(1, 1).Value = "Processing " & Partner
Application.ScreenUpdating = True
Application.ScreenUpdating = False

After this, I return to whatever sheet I need. 在此之后,我回到我需要的任何表格。 Nothing fancy, but it's fast, easy, and does what I need. 没有什么花哨的,但它快速,简单,并且做我需要的。 :) :)

I use a command button on the form set to a very small size, hidden when the form loads, with the caption "Please wait, processing..." 我使用表单上的命令按钮设置为一个非常小的大小,在表单加载时隐藏,标题为“请稍候,处理......”

I resize the button in the form Iniliatlise event (I simply resize to the form size using the top, left, width & height properties). 我在Iniliatlise事件中调整按钮的大小(我只需使用top,left,width和height属性调整表单大小)。 YOu can of course make it whatever size you like. 你当然可以做任何你喜欢的尺寸。

I make the command button visible just before my time consuming code & make it hidden again at the end of the code. 我在耗费代码之前使命令按钮可见,并在代码末尾再次隐藏它。

Requires some "DoEvents", but works perfectly. 需要一些“DoEvents”,但效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM