Access TextBox 在 VBA 循环期间不会更新,只有在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27798152/
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-12 05:43:10  来源:igfitidea点击:

Access TextBox does not update during VBA loop, only after

vbams-accesstextboxaccess-vba

提问by Emil Olsen

In access using VBA.

在使用 VBA 访问。

I have a quite simple situtation, and I see some different solutions online, but non of them works for me.

我有一个非常简单的情况,我在网上看到了一些不同的解决方案,但没有一个对我有用。

I have a form with a textbox that needs to be updated in a for loop. I have reduced the script to the following:

我有一个带有文本框的表单,需要在 for 循环中更新。我已将脚本简化为以下内容:

For counter = 1 To 100

Application.Echo False
DoCmd.OpenQuery "Query1"
DoCmd.Close
Application.Echo True

strStatus = "Loop is " & counter & " % done"
Call dsp_progress_AfterUpdate
Me.Refresh
DoEvents

Next

And the sub thats called:

和子那叫:

Private Sub dsp_progress_AfterUpdate()
Me.Dirty = False
End Sub

The textbox controlsource is the strStatus (Through a function).

文本框控件源是 strStatus(通过函数)。

Every loop takes about 4 seconds (The query), so it is not because its over in 2 ms.

每个循环大约需要 4 秒(查询),所以不是因为它在 2 毫秒内结束。

It only updates when the for loop is finished.

它仅在 for 循环完成时更新。

The strange thing is, if i use the curser and manually click on the text box while the loop is running, it actually works.........

奇怪的是,如果我使用光标并在循环运行时手动单击文本框,它实际上可以工作.......

So the question is, how do i make it update "live" without having to click on the textbox with the mouse/curser? That is not convenient for a status display.

所以问题是,如何让它更新“实时”而不必用鼠标/光标点击文本框?这对于状态显示来说是不方便的。

Help, please... :)

请帮忙... :)

Best Regards, Emil.

最好的问候,埃米尔。

回答by KevenDenen

I'm not sure what the root cause of it failing to update the text box is, but adding in one line fixed this issue for me. Setting focus to the textbox that you are updating (which is what you are doing when you click on it), causes it to update.

我不确定无法更新文本框的根本原因是什么,但添加一行为我解决了这个问题。将焦点设置到您正在更新的文本框(这是您单击它时正在执行的操作),会导致它更新。

textBox.SetFocus

Add that in to your code before starting the loop.

在开始循环之前将其添加到您的代码中。

Changing the code to the following should get rid of the flickering and the fact that the text is highlighted.

将代码更改为以下应该消除闪烁和文本突出显示的事实。

For counter = 1 To 100
    Application.Echo False
    DoCmd.OpenQuery "Query1"
    DoCmd.Close
    strStatus = "Loop is " & counter & " % done"
    Call dsp_progress_AfterUpdate
    Me.Refresh
    TextBox.SetFocus
    TextBox.SelStart = 0
    TextBox.SelLength = 0
    DoEvents
    Application.Echo True
Next