如何确定哪个控件具有焦点,MS Access VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3515841/
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
How to identify what control has focus, MS Access VBA
提问by Icode4food
The question says it all.
问题说明了一切。
I have a listbox whose rowsource is change programmaticaly. If the control has the focus then I need use me.txtSearch.text
when building the rowsource, otherwise I need to use me.txtSearch.value
.
我有一个列表框,其行源以编程方式更改。如果控件具有焦点,则me.txtSearch.text
在构建行源时需要使用,否则我需要使用me.txtSearch.value
.
How do I determine what control has the focus?
如何确定哪个控件具有焦点?
回答by Fionnuala
Screen.ActiveControl
is the control that has focus
是有焦点的控件
回答by David-W-Fenton
I understand your question, but you seem to me to be asking the wrong thing. I'd think that if you're altering a rowsource based on a value typed into a textbox, you'd be doing it from the AfterUpdate of Me!txtSearch, in which case the control has the focus and .Text and .Value are identical. It's only in the OnChange event that .Text and Value may be different. You might also find OldValue and NewValue useful, though those only work with bound controls.
我理解你的问题,但在我看来你问错了。我认为,如果您根据输入到文本框中的值来更改行源,那么您将从 Me!txtSearch 的 AfterUpdate 中执行此操作,在这种情况下,控件具有焦点,并且 .Text 和 .Value 是完全相同的。只有在 OnChange 事件中 .Text 和 Value 可能不同。您可能还会发现 OldValue 和 NewValue 很有用,尽管它们只适用于绑定控件。
Of course, that applies only to textboxes -- combo boxes are different, in that the .Value property is the bound control, and the .Text property is the first displayed column (i.e., the values you choose from in the list, the ones that AutoComplete works on). You don't need to use the .Text property to get that value. Instead, you can use Me!cmbSearch.Column(1) (assuming Column(0) is the bound column and is zero width, i.e., hidden).
当然,这只适用于文本框——组合框是不同的,因为 .Value 属性是绑定控件,而 .Text 属性是第一个显示的列(即,您从列表中选择的值,那些自动完成工作)。您不需要使用 .Text 属性来获取该值。相反,您可以使用 Me!cmbSearch.Column(1)(假设 Column(0) 是绑定列并且宽度为零,即隐藏)。
So, in general, I don't see why you have any need to distinguish between .Value and .Text because they will be the same in all events except OnChange. The exception is if the control is a combo box and in that case, you don't need to use .Text at all, but use .Column(1).
因此,总的来说,我不明白为什么您需要区分 .Value 和 .Text,因为它们在除 OnChange 之外的所有事件中都是相同的。例外情况是如果控件是一个组合框,在这种情况下,您根本不需要使用 .Text,而是使用 .Column(1)。
If I'm guessing right, you are collecting criteria for a WHERE clause of a combo box RowSource, and you want to write code to do it from any of the textboxes you're using to collect the criteria. In that case, the .Value of the controls is sufficient (you don't even need to specify it, as it's the default property of all bound Access controls). There is no situation in which .Text is going to be different from .Value that you would be writing your RowSource.
如果我猜对了,您正在收集组合框 RowSource 的 WHERE 子句的标准,并且您想编写代码以从用于收集标准的任何文本框中执行此操作。在这种情况下,控件的 .Value 就足够了(您甚至不需要指定它,因为它是所有绑定 Access 控件的默认属性)。没有任何情况下 .Text 会与您编写 RowSource 的 .Value 不同。
Of course, I could be wrong about any number of assumptions about what you're actually trying to do, but since you didn't explain that, I'm forced to guess, as the question itself really doesn't make sense and is pretty much beside that point. That is, there's no benefit from using the .Text property, so no need to worry about which control has the focus.
当然,关于您实际尝试做的任何数量的假设,我都可能是错误的,但是由于您没有对此进行解释,因此我不得不猜测,因为问题本身确实没有意义并且是几乎在这一点之外。也就是说,使用 .Text 属性没有任何好处,因此无需担心哪个控件具有焦点。
回答by jcolebrand
I would do it the dumb way because I've never seen such an API for Access. Just grab Form.Controls and cycle through them.
我会用愚蠢的方式来做,因为我从未见过这样的 Access API。只需抓住 Form.Controls 并循环浏览它们。