VBA:为什么我每次都必须设置焦点来控制?

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

VBA: Why must I set focus to control every time?

vbams-accesspropertiesms-access-2007access-vba

提问by Analytic Lunatic

I am creating a personal Library Inventory system using an Access 2007 database. In code, whenever I reference the .Textproperty of a form control, whether it be changing the value, or simply checking the value in an IF statement, I get prompted with Run-time error '2185': You can't reference a property or method for a control unless the control has the focus.

我正在使用 Access 2007 数据库创建个人图书馆库存系统。在代码中,每当我引用.Text表单控件的属性时,无论是更改值,还是只是检查 IF 语句中的值,我都会收到Run-time error '2185': You can't reference a property or method for a control unless the control has the focus.

Why is this?

为什么是这样?

For setting the .Textit's not a huge deal, but when I'm checking the value in an IF statement, I can't set the focus when I'm checking multiple conditions.

对于设置.Text它并不是什么大问题,但是当我检查 IF 语句中的值时,我无法在检查多个条件时设置焦点。

回答by Geoff

Use .Valueinstead - that doesn't require setting focus first. From the documentation, for example for the TextBox control(emphasis mine):

使用.Value代替-这并不需要首先设置焦点。从文档中,例如对于TextBox 控件(强调我的):

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 unavailable until the control gets the focus again.

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