Up down arrow keys only work on list box after a mouse click...

R

RzB

I have an unbound list box on a form. When the
form opens I run the following code...

'Make sure list box is enabled
Me.lstSelector.Enabled = True

'Select the top item in the list box
Me.lstSelector = Me.lstSelector.ItemData(0)

'Ensure list box has focus
Me.lstSelector.SetFocus

My problem is that the up down arrow keys
do not function on the list box. If I click on
an item in the list box with the mouse it works
just fine. Furthermore, once I have clicked with
the mouse the up down keys operate correctly...

How do I get the keys to work without having
to do a mouse click first?

Many thanks,
Roy
 
W

Wayne Morgan

Is the listbox set for Multiselect Simple or Extended? If yes, you can't
just assign a value to it. You have to use the Selected property of the
listbox and set it to True for the desired row.

If the listbox isn't Multiselect, then you can set a value as you've done.
Which event are you doing this in? If you're doing this in the form's Open
event, it may be too soon, many parts of the form aren't ready at this
point. Try the Load event instead. I just tried this with a non Multiselect
listbox using the Load event and it worked as you say you want it to.
 
R

RzB

Wayne,
Many thanks for your help.
No it isn't multi select....
Here ia a picy of the list box properties..

http://www.gillandroy.com/accessprobs

Yes the code is in the form open event. I have moved
it to the load event but it makes no difference.

The form opens, the top items in the list box is highlighted
with a black background but the up/down arrow keys
won't work till I have clicked on one of the items in the
list box. It's almost as if it hasn't got focus till I click on it...
Hmmm...

Many thanks,
Roy
 
W

Wayne Morgan

I didn't see a "header" row, did I miss one or is there not one? If there is
one, then Row(0) would be the header row and that may cause a problem.

You may want to make the SetFocus the last line (before Exit Sub or error
handlers) in the Load event. Also, is there any code running in a timer
event of this form or another open form?
 
R

RzB

Nope - no header row...

The set focus was the last line prior to the exit sub of the form load
handler. Apart from error handling code. But the same happens even when I
comment out the error handling code..

Interestingly I can't even tab off the list box until I've clicked with the
mouse...

No - No time code whatsoever...

Hmm..

Thanks for your help...
Roy
 
W

Wayne Morgan

When tested here, it works. I wonder if the control is corrupted. Try
creating a new, blank form and put a couple of different controls on it,
including a listbox and try what you're doing there and see if it works. Put
in just enough code to select the row in the listbox and set focus to the
listbox.
 
S

Stephen Lebans

Wayne the ability to set focus to a ListBox control in the Form's Load
event is not 100% reliable as the control is not always fully
instantiated at this point. Using a tool such as SPY++ you can see that
the ListBox window exists but it is still not visible. I realize you are
not seeing this behaviour on your machine but I have done extensive
testing with this issue.

Perhaps the OP could setfocus to an existing TextBox control and then
immediatedly setfocus to the ListBox control. I have seen this resolve
the issue in many cases.

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
W

Wayne Morgan

Thanks Stephen,

I hate timing issues. Thanks for the tip, I hadn't heard of that problem
before.
 
W

Wayne Morgan

Stephen,

I got to thinking. Do you think it would work to set a flag in the Load
event then check for this flag in the Current event and set focus there,
then clear the flag? The published event order is Open ? Load ? Resize ?
Activate ? Current. Of course, Activate may work also if the user wants the
focus to return to this control every time they return to the form.
 
R

RzB

Guys, Just to let you know I am still looking at this - have been "out of
the office" earlier... I'm trying some of the things you have mentioned
above...

Many thanks for your help. I't really nice to have someone to bounce things
around with when you are working alone..

Perhaps I should have mentioned before that the form is a sub form on a
Tabbed form. When the tab is selected I set up the sub form by filling in
the SourceObject with the form name - thus causing it to burst into life...
When the form is out of site it is closed by removing the SourceObject.

I have just tried running the sub form on it's own (not selected from the
tabbed form) by double clicking on it in the database window, and it workd
correctly.. ie - the list box reacts to keyboard input ok.

So I'm currently crawling through the code starting from when the tab gets
selected to see what might have an effect.

Will report back later.

Roy
 
W

Wayne Morgan

Yes, a subform would make a difference. I assume the code is in the
subform's event. This would set the focus to the control on the subform but
NOT set the focus to the subform itself. Whatever control on the main form
gets the initial focus will be the control with the focus and so you're
up/down arrow keys will try to act on it, not on the listbox in the subform.

To set the focus to a control on a subform is a two step process, first set
the focus to the subform control on the main form then set the focus to the
control on the subform. The subform control is a container control on the
main form. This is the control where you set the Parent/Child links. You
will need the name of this control, it may or may not be the same as the
name of the subform itself.

If the code is running on the main form:
Me.NameOfSubformControl.SetFocus
Me.NameOfSubformControl.Form.ctlControlOnSubform.SetFocus

If the code is running on the subform, change Me to Me.Parent in the first
line. You then should be able to set the focus to the listbox as normal
(Me.ctlOnSubform.SetFocus). This has the potential to have lots of timing
problems though since the main form and subform may not load in the order
desired. You may have to use the flag I mentioned in my previous post and
set the focus in the main form's Current event to give adequate time for
everything to be loaded.
 
R

RzB

Wayne,
OK - I am close to giving up on this one.... :)

I have been round and round trying all sorts of options,
honing my trace code etc... The system runs absolutly as
expected timing wise - I can trace/time procedure entry
and exit and there appears to be nothing unusual going on...

I am now convinced that it is something to do with the fact
that I using tabedd controls. The form I'm trying to set focus
to is a subform on a tabbed control. The tabbed control is itself
a subform sitting on a tab of a tabed control...

Anyway... I have created a very simple mini "Test" database
that shows the problem I'm facing - with just one tabbed control..

It can be foiund at ........

http://pix.gillandroy.com/accessprobs/testdb.mdb

Its a simple Tabbed form with two tabs and a subform
on each tab. There is a text box and button on each subform.
The code tries to set focus to the text box using the me.parent
method you suggested... but it don't work. I have tried all
sorts but just can't get it to work...

If you have a moment to take a look that would be great,,,

I'm probably doing something really daft...

Many thanks,
Roy
 
W

Wayne Morgan

I've downloaded your database and am playing with it. You appear to be
correct that the problem is the tab control. I tried just a plain textbox on
the tab page (no subform) and the problem still exists. I'll let you know if
I come up with anything.
 
W

Wayne Morgan

PS.

If you want kind of the same look, you may be able to use toggle buttons
placed side by side at the edge of the tab control, that way they'll stay
depressed when that page has the focus, but you'll also have to code them
back up as the focus moves from page to page.
 
R

RzB

Wayne,

Hmmm - a tab control without tabs seems a bit daft to me... :)

However I have persevered - thought about it all night !

I have a fix - not at all elegent but a fix...

See....

http://pix.gillandroy.com/accessprobs/testdbfixed.mdb

Now all I have to do is make it work properly in my
application with nested tab controls...hmmm.. Monday's job :)

I would be interested to see if you can think of any
improvements to this...

Thanks,
Roy
 
R

RzB

Hmm - I realise I haven't explianed what I did...

I have created a new form... Nothing on it, minimal spec
as far as properties go and no code behind it.
Also it is very tiny... In fact I don't ever want the user
to see it...

After setting the .SourceObject I open this tiny form -
I think this has the effect of moving the focus away
from the tabbed control ? Yes/No ?,
then do the setfocus stuff, then close the form...

Select Case strPageName
Case "pgeForm1"
Me.sfrmForm1.SourceObject = "subForm1"
DoCmd.OpenForm "Form3"
Me.sfrmForm1.SetFocus
Me.sfrmForm1.Form.Text0.SetFocus
DoCmd.Close acForm, "Form3"

The form is so small and it all happens so fast that
the user does not see the open form.
Hey presto all seem to work ok !

I have a fast machine and it all works OK. I need to try it
on a slower machine to see if it is acceptable. Perhaps there
is still a better way yet..?

Roy
 
R

RzB

Hmm... have detected a further simplification....

If I open the "nearly invisible" form before the
setting of theSourceObject, then the control that
is first in the subforms tab index gets focus automatically...
I do not have to do the setfocus commands!

Thus....

DoCmd.OpenForm "Form3"

Select Case strPageName
Case "pgeForm1"
Me.sfrmForm1.SourceObject = "subForm1"

Case "pgeForm2"
Me.sfrmForm2.SourceObject = "subForm2"

Case Else
End Select

DoCmd.Close acForm, "Form3"

Interesting...

See...
http://pix.gillandroy.com/accessprobs/testdbfixed2.mdb

Roy
 
W

Wayne Morgan

Good work around. I had noticed that if I clicked off of the form and back
again that the focus was where desired. If the screen flashes or the other
form is visible, you may be able to turn Echo off for a moment while you do
this then turn Echo on again after you close the other form.
 
R

RzB

Wayne,
Yes I had noticed that too... that's how I got to a fix...

Yes, Echo off helps a little but there is a flicker as soon as the tab
control gets clicked. I can get rid of that by putting the Echo off in the
On Exit of the subform control that is losing focus. That makes it look very
smooth. Unfortunately it's a nightmare keeping track of if the status and
I'm sure I would end up with Echo stuck off... Especially in erring
conditions. The user will have to put up with a little flicker :)

Many thanks for your help,
Roy
 
W

Wayne Morgan

To handle error conditions, turn echo on in your error handler. At that
point, handling the error is more important than it looking pretty.
 
Top