tags:

views:

55

answers:

3

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.

How do I determine what control has the focus?

A: 

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.

drachenstern
Why don't you delete that answer now that you've seen it does exist?
David-W-Fenton
@David-W-Fenton ... I think it's been established that this doesn't work for Forms that don't have current top-window status. There is still the option to have each form have an active control, but the API only works for the most active form/control.
drachenstern
Since Access controls don't have a window handle except when they have the focus, I doubt you can find out which control would have the focus in a form that does not itself have the focus (unless you select each form in turn and check which control has focus). I'm also not sure why you'd want to know that, let alone what it has to do with the original question. Unless you expand your discussion of the issues, I can't see what value your answer adds -- as it stands, it's factually incorrect, so you should either beef it up to make it valuable, or delete it to up the signal-to-noise ratio.
David-W-Fenton
I'm pretty sure that every window in Access has a handle. I'll have to follow up on that at some point in the future. But I've seen plenty of VBA code that requires the form handle, and can be used with hidden forms, IIRC. ~ As for why you would want to find the control with focus on forms that don't have focus, idk, but I'm not that familiar with all the world's dumb requests, or all the problems that need to be solved. I can imagine that it would be a good exercise. Perhaps you should try it without using a form variable?
drachenstern
+1  A: 
 Screen.ActiveControl

is the control that has focus

Remou
Can each form not have an individual focus for when you switch between forms? Like if I have two forms open, and each has three textboxes, and I am in the second textbox on the first form, and goto the third textbox on the second, it will forget my place on the first one? I've never experimented with that so idk...
drachenstern
The first form and textbox loose focus and the second form gets focus (screen.activeform)
Remou
@Remou ~ Yeah, I gathered that when I click on form2, but when I go BACK to form1 .. who gets focus?
drachenstern
It depends on how you get there: forms!form1.setfocus will give you the original control, but clicking might lead to anything :)
Remou
If Control1 has the focus on Form1 and you switch to Form2 and ControlB gets focus, when you switch back to Form1, Control1 will still have the focus, barring user interaction, of course. That is, changing which form has the focus does not alter which control on each control has the focus (unless there's an OnActivate event that sets focus to a particular control, for instance).
David-W-Fenton
A: 

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.

David-W-Fenton