How to identify what control has focus, MS Access VBA
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.开发者_Python百科value
.
How do I determine what control has the focus?
Screen.ActiveControl
is the control that has focus
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.
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).
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).
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.
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.
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.
精彩评论