Lost Focus Woes

G

Guest

When the lost focus event occurs on a combo box, I want to set the focus
elsewhere. The error message says I can't change the focus on something that
has the focus. I thought it lost the focus. And you can't hide it either
because it still has the focus. I guess it hasn't lost the focus until after
the lost focus event takes place. So, how can I get control when someone
clicks off the combo box yet hasn't made a selection yet?

This is the same question worded another way. Basically, I have a text box
on top of a combo box. When the text box gets the focus, it unhides the
combo, switches focus to the combo, and hides the text box. However, the
opposite is not allowed. When the combo looses focus, I want to unhide the
text box, give it the focus, and hide the combo. The error message states
that I can't hide nor change focus on something that has the focus. Yet I am
in the lost focus module, is there another place I can gain control?
 
M

Matthias Klaey

Michael Conroy said:
When the lost focus event occurs on a combo box, I want to set the focus
elsewhere. The error message says I can't change the focus on something that
has the focus. I thought it lost the focus. And you can't hide it either
because it still has the focus. I guess it hasn't lost the focus until after
the lost focus event takes place. So, how can I get control when someone
clicks off the combo box yet hasn't made a selection yet?

This is the same question worded another way. Basically, I have a text box
on top of a combo box. When the text box gets the focus, it unhides the
combo, switches focus to the combo, and hides the text box. However, the
opposite is not allowed. When the combo looses focus, I want to unhide the
text box, give it the focus, and hide the combo. The error message states
that I can't hide nor change focus on something that has the focus. Yet I am
in the lost focus module, is there another place I can gain control?

What is the purpose of this text field on top of the combo box? What
do you want to achieve with this?
Maybe there is a simpler solution.

What version of Access do you use?

Kind regards, Matthias Kläy
 
S

storrboy

When the lost focus event occurs on a combo box, I want to set the focus
elsewhere. The error message says I can't change the focus on something that
has the focus. I thought it lost the focus. And you can't hide it either
because it still has the focus. I guess it hasn't lost the focus until after
the lost focus event takes place. So, how can I get control when someone
clicks off the combo box yet hasn't made a selection yet?

This is the same question worded another way. Basically, I have a text box
on top of a combo box. When the text box gets the focus, it unhides the
combo, switches focus to the combo, and hides the text box. However, the
opposite is not allowed. When the combo looses focus, I want to unhide the
text box, give it the focus, and hide the combo. The error message states
that I can't hide nor change focus on something that has the focus. Yet I am
in the lost focus module, is there another place I can gain control?


I think you may be attempting to create a sort of endless loop. If you
have code that executes when the textbox gets focus, then it will
execute when you use TextBox.SetFocus. If that code tries to
manipulate the combo you're just going back and forth. Depending on
what you are trying to do, you may need to involve other control focus
events instead of the ones you are trying to affect.
 
G

Guest

It makes for a nicer form. For example, I want the text box to display an
employee as firstname then lastname, however, the combo box does the
searching lastname then firstname. Once the selection has been made, the
combo is hidden and the new selection is displayed properly in the text box.
I am having trouble hiding the combo when the combo looses the focus.
Currently, when you click on something else, it checks the visibility of the
combo and hides it, but I don't want to put an IF statement in every other
field on the form. I was hoping a lost focus type of action was possible and
that is what I am looking for.
 
G

Guest

I agree, it is a loop and that may be why MS doesn't allow this. However,
suppose I set the focus somewhere else when the combo looses control, say to
the form's close button. I would still get the error that I can't change
focus from the combo to something else, while the combo has the focus. And
this is in the Lost Focus event. But you may have hit on the answer. If the
user goes to click on the last name field from the first name field, the lost
focus event could redirect you somewhere else before the click is allowed to
happen. Maybe the lost focus is disallowed from setting the focus elsewhere
because the very nature of loosing the focus means you clicked somewhere else
and you want to go there, not where the code sends you. I think I will have
to live with it this way and figure out another workaround. Thanks
 
M

Matthias Klaey

Michael Conroy said:
I agree, it is a loop and that may be why MS doesn't allow this. However,
suppose I set the focus somewhere else when the combo looses control, say to
the form's close button. I would still get the error that I can't change
focus from the combo to something else, while the combo has the focus. And
this is in the Lost Focus event. But you may have hit on the answer. If the
user goes to click on the last name field from the first name field, the lost
focus event could redirect you somewhere else before the click is allowed to
happen. Maybe the lost focus is disallowed from setting the focus elsewhere
because the very nature of loosing the focus means you clicked somewhere else
and you want to go there, not where the code sends you. I think I will have
to live with it this way and figure out another workaround. Thanks

What if you use the Exit event instead of the LostFocus event?
I don't know if this works, I didn't test this.

Greetings
Matthias Kläy
 
S

storrboy

...I would still get the error that I can't change
focus from the combo to something else, while the combo has the focus.

I don't think so.
In my limited test of what I think you are doing, the focus moves back
and forth as expected, but at some point I think it overlaps, or gets
caught somehwere
The LostFocus sub as I've done is as follows and the textbox does
become visible and the combo hidden. I set up the form to have the
textbox hidden to start.

Private Sub Combo0_LostFocus()
Me!Text2.Visible = True
Me!Text2.SetFocus
Me!Combo0.Visible = False
End Sub
Maybe the lost focus is disallowed from setting the focus elsewhere ...

It's not that what you are doing is disallowed so much as conflicting
and causing an error. VB dosen't control loops. It's quite easy to
make an endless loop that does not cause an error ( I probably should
have used a different term). For instance (if you try this it will go
on forever and you'll have to use Ctrl+Break to stop it.)...

a=1
b=2

Do Until a>b
c=c
Loop

As I implied - I'm not sure of how exactly you have this setup to work
so to give real advice is difficult.
I'm guessing you are doing this based on the value of some other field
in order to provide a dropdown selection instead of a textbox when
it's appropriate? For example 2 textboxes and 1 combo set up something
like...

<<TextBoxFieldName>> <TextBoxTextValue><ComboValue>

The right two are what you are trying to hide/unhide. I might try
using the Exit or LostFocus event of <<TextBoxFieldName>> to hide/
unhide the other two as needed. In the LostFocus of the combo, set the
focus back to <<TextBoxFieldName>> or some other control other than
the affected two and hide/unhide the others. This way a loop can't
occur as it's always when a control looses focus.
 
G

Guest

I figured out the problem. If you look under the format menu you will see
bring to front and back commands. By bringing the combo box to the back, thus
making the text box on top, when you unhide the text box it will show up on
top of the combo box, so I don't have to hide the combo on the lost focus
event. On the combo's lost focus, just unhide the text box and it will be on
top which makes it look like the combo has been hidden when it is really just
underneath. I have a form reset module that upon opening will hide all lookup
combos and unhide all overlaying text boxes, so this solution works for me.
Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top