vba 在更改事件处理程序中获取文本框值

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

Obtaining textbox value in change event handler

ms-accessvbaaccess-vba

提问by Kaganar

I've written a form that performs queries asynchronously as text is typedinto a textbox, however I somewhat arbitrarily seem to get the following error thrown: "You can't reference a property or method for a control unless the control has focus."

我编写了一个表单,当文本输入到文本框中时异步执行查询,但是我似乎有点武断地抛出以下错误:“除非控件具有焦点,否则您无法引用控件的属性或方法。”

The immediately relevant code is:

直接相关的代码是:

Dim UpdateRequested As Boolean
Dim qryText As String

.
.
.

Private Sub txtBox_Change()
    qryText = txtBox.Text
    UpdateRequested = true
End Sub

Some place in the ellipses is the code that handles dynamically loading an ADODB record set, populating a local table, and refreshing a sub form. However, even when I disable this code, the problem persists: Sometimes I get the error. Sometimes I do not.

省略号中的某些地方是处理动态加载 ADODB 记录集、填充本地表和刷新子表单的代码。但是,即使我禁用此代码,问题仍然存在:有时我会收到错误消息。有时我不这样做。

This seems to be persistent through closing the database and reopening it. Every time it starts working again, it's because I've been fooling around with code in the debugger, but I'm not sure what exactly is causing it to magically "just work" or what is causing it to not work at all.

通过关闭数据库并重新打开它,这似乎是持久的。每次它再次开始工作时,都是因为我一直在调试器中玩弄代码,但我不确定究竟是什么导致它神奇地“正常工作”,或者是什么导致它根本无法工作。

Update

更新

Just to make things more puzzling, I added a couple of simple event handlers:

为了让事情更令人费解,我添加了几个简单的事件处理程序:

Private Sub txtBox_GotFocus()
    MsgBox "Got focus"
End Sub

Private Sub txtBox_LostFocus()
    MsgBox "Lost focus"
End Sub

I run the form. I click in the test box. I receive the "Got focus" message. As soon as I type I see the error as described above. If I re-open the form, I can click between the text box in question (which itself is unbound) and a bound text box in the sub form and see both "Got focus" and "lost focus" messages as one would expect. Furthermore, showing a message box with the current value of "Screen.ActiveControl.Name" shows the expected name just before the Text property is accessed.

我运行表格。我点击测试框。我收到“获得焦点”消息。一旦我输入,我就会看到如上所述的错误。如果我重新打开表单,我可以在有问题的文本框(它本身是未绑定的)和子表单中的绑定文本框之间单击,并按预期看到“获得焦点”和“失去焦点”消息。此外,显示当前值为“Screen.ActiveControl.Name”的消息框会显示访问 Text 属性之前的预期名称。

回答by wastrel

I know this is an old thread but it's the first I found when I had the same problem. None of the answers helped except Kaganar's own solution, which pointed me in the right direction. I'm guessing the reason people had trouble reproducing the error is there are some important details missing from Kaganar's description:

我知道这是一个旧线程,但这是我遇到相同问题时发现的第一个线程。除了 Kaganar 自己的解决方案外,所有答案都没有帮助,这为我指明了正确的方向。我猜人们无法重现错误的原因是 Kaganar 的描述中缺少一些重要的细节:

  1. The Textbox was in the form header (or footer).
  2. The form did not allow additions.
  1. 文本框位于表单页眉(或页脚)中。
  2. 该表格不允许添加。

Because I believe the full answer is...

因为我相信完整的答案是......

The Text property of any control is inaccessible when the form has a record source with no records to edit

当窗体具有没有要编辑的记录的记录源时,任何控件的 Text 属性都无法访问

I think there is part of Access that does not realise the textbox exists :) To understand how that might come about...

我认为 Access 的一部分没有意识到文本框的存在:) 要了解这可能是如何发生的......

  • Put the unbound TextBox in the detail of the form
  • Do not allow additions
  • Set the recordsource to return no records
  • Open the form.
  • 将未绑定的TextBox放在表单的详细信息中
  • 不允许添加
  • 设置记录源不返回记录
  • 打开表格。

Hey presto! No Textbox.

嘿快点!没有文本框。

Return a record, or allow additions, or delete the recordsource, et Voila! There is your Textbox with it's Text.

返回记录,或允许添加,或删除记录源,等等!有你的文本框和它的文本。

回答by HansUp

I added a text box named txtFooto my form. Here is the procedure for its change event.

txtFoo在表单中添加了一个名为的文本框。这是其更改事件的过程。

Private Sub txtFoo_Change()
    Debug.Print "Value: " & Nz(Me.txtFoo.value, "*Null*") & _
        "; Text: " & Nz(Me.txtFoo.Text, "*Null*")
End Sub

Then, with nothing in txtFoo(IOW its value is Null) when I type "abc" into txtFoo, here is what I see in the Immediate window.

然后,txtFoo当我在 中键入“abc”时(IOW 其值为 Null)中没有任何内容txtFoo,这就是我在“立即”窗口中看到的内容。

Value: *Null*; Text: a
Value: *Null*; Text: ab
Value: *Null*; Text: abc

Basically, each character I add to the text box triggers its change event and prints the text box's current contents to the Immediate window.

基本上,我添加到文本框的每个字符都会触发其更改事件并将文本框的当前内容打印到立即窗口。

As far as I understand, you want to do something similar ... except you want a different action in place of Debug.Print. Take another look at your change event procedure and compare it to mine.

据我了解,你想做一些类似的事情......除了你想要一个不同的动作来代替Debug.Print. 再看看你的变更事件过程,并将其与我的进行比较。

Private Sub txtBox_Change()
    qryText = txtVendorName.Text
    UpdateRequested = true
End Sub

That is the change event for a control named txtBox. Within that procedure you reference the .Textproperty of a control named txtVendorName. However txtBoxis the active control at the time its change event code runs ... so you can not access the .Textproperty of txtVendorNamebecause it is not the active control.

这是名为 的控件的更改事件txtBox。在该过程中,您引用.Text名为 的控件的属性txtVendorName。但是txtBox,在其更改事件代码运行时是活动控件……因此您无法访问 的.Text属性,txtVendorName因为它不是活动控件。

Given that this problem surfaces for only the one form, but not on other new forms, I would suspect the problem form has become corrupted. Read the 2 answers to this SO question and try decompile to cure the corruption: HOW TO decompile and recompile. Decompile is often recommended as a routine practice during development.

鉴于此问题仅在一种形式上出现,而在其他新形式上没有出现,我怀疑问题形式已损坏。阅读这个 SO 问题的 2 个答案并尝试反编译以修复损坏:如何反编译和重新编译。反编译通常被推荐为开发过程中的常规做法。

You could also use the undocumented Application.SaveAsTextmethod to save your form as a text file. Delete the bad form, and use Application.LoadFromTextto import the saved text copy.

您还可以使用未记录的Application.SaveAsText方法将表单保存为文本文件。删除坏表格,并用于Application.LoadFromText导入保存的文本副本。

Make sure you have a backup copy of your db file in case anything goes wrong.

确保您有 db 文件的备份副本,以防出现任何问题。

回答by SeanC

To set or return a control's Text property, the control must have the focus, or an error occurs.
To move the focus to a control, you can use txtBox.SetFocusor DoCmd.GoToControl "txtBox".

要设置或返回控件的 Text 属性,控件必须具有焦点,否则会发生错误。
要将焦点移动到控件,您可以使用 txtBox.SetFocusDoCmd.GoToControl "txtBox"

Also, the Text property is not always available:
While the control has the focus, the Text property contains the text data currently in the control; the Value property contains the last saved data for the control. When you move the focus to another control, the control's data is updated, and the Value property is set to this new value. The Text property setting is then unavailableuntil the control gets the focus again.

此外,Text 属性并不总是可用:
当控件具有焦点时,Text 属性包含当前在控件中的文本数据;Value 属性包含控件上次保存的数据。当您将焦点移到另一个控件时,该控件的数据会更新,并且 Value 属性被设置为这个新值。在控件再次获得焦点之前,Text 属性设置将不可用

回答by Kaganar

The form had a lingering data source. I'm not sure why this would cause to the behavior described above, especially considering the text box controls are unbound, however since removing the data source the text boxes are behaving as expected.

该表单有一个挥之不去的数据源。我不确定为什么这会导致上述行为,特别是考虑到文本框控件未绑定,但是由于删除数据源,文本框的行为符合预期。

回答by Omid Sadeghi

You said "somewhat arbitrarily"I think if everything is fine you must get the error when your form's recordset is empty.

您说“有点武断”,我认为如果一切正常,当表单的记录集为空时,您一定会收到错误消息。

In fact it's a know bug in Access and this error can occur if these conditions are met: a) The control is in the Form Header or Form footer section b) The form is filtered such that no records match (or there are no records) c) No new record can be added.

事实上,这是 Access 中的一个已知错误,如果满足这些条件,就会发生此错误:a) 控件位于表单页眉或表单页脚部分 b) 表单被过滤,以便没有记录匹配(或没有记录) c) 不能添加新记录。

In this case, the Detail section of the form goes blank. The controlis still visible, but Access gets really confused and can throw the error you describe.

在这种情况下,表单的详细信息部分变为空白。控件仍然可见,但 Access 真的很困惑,可能会抛出您描述的错误。

More info: http://allenbrowne.com/bug-06.html

更多信息:http: //allenbrowne.com/bug-06.html

回答by C0L0mb0

I know my answer is out of date. Yet you just can set focus three times. On TextBox in header, on any texbox in detail space and On TextBox in header again. I use access 2003.

我知道我的答案已经过时了。但是你只可以设置焦点三遍。在标题中的文本框上,在细节空间中的任何文本框上,再次在标题中的文本框上。我使用访问 2003。