List box returns wrong value

  • Thread starter Thread starter Hakan Naslund
  • Start date Start date
H

Hakan Naslund

Hi!

I have a listbox configured as Extended Multi Select.

On the After Update Event, I have this code:
Private Sub List0_AfterUpdate()
For Each itm In List0.ItemsSelected
Debug.Print List0.ItemData(itm)
Next itm
End Sub

If I use the mouse to select records (just single records, no ctrl or
shift!), the
debug.print return the correct row and data.

BUT if I use the keyboard arrow keys to move up/down in the listbox,
the debug.print will show the values of the row that was active before the
up/down move!

Let's say my list box shows the following rows:
Andrew
Burt
Charlie
Derek

A mouseclick on Burt will debug.print '1 Burt'
A mouseclick on Charlie will debug.print '2 Charlie'

If I now use the keyboard's down key to move to Derek, it will still print
'2 Charlie' !
And if I press the up key and move back to Charlie, it prints '3 Derek'.

So when using the keyboard I'm always one step behind what's actually
marked...

Any ideas?

Hakan Naslund
Sweden
 
Sorry, I don't follow you here...

GotFocus would only trigger when the control gets focus (as the name
declare), and I need to trigger on whenever the value of the control
changes.

Just by moving up/down in a list box will never make it loose it's focus...

The list box if my link master field to a subform and I need to execute some
code everytime a different record is selected by the list box.

Hakan
 
Hi Arvin!

I'm using the keyboard up/down-arrow keys to move...

Both using the mouse or the arrow keys triggers the AfterUpdate event and it
should not matter which one of the methods I'm using, the ItemsSelected
value should return the correct value! Don't you think?

I don't see why a listbox should ever return the value of another item than
the one obviously selected on the screen.

Have you tried the ListBox.mdb I attached?

Hakan
 
I've been using the click event of a simple multiselect (lstElevation) to
fill a text box (txtSelected) for use in an IN clause of a SQL statement,
like this:

Dim varItem As Variant
Dim strList As String

With Me!lstElevation
If .MultiSelect = 0 Then
Me!txtSelected = .Value
Else
For Each varItem In .ItemsSelected
strList = strList & .Column(0, varItem) & ","
Next varItem
If strList <> "" Then
strList = Left$(strList, Len(strList) - 1)
End If
Me.txtSelected = strList
End If

End With
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 
Using this code on a EXTENDED MultiSelect ListBox gets the same result as I
have...

Hakan
 
I seem to remember seeing this before. I think I just gave up and went to a simple select
box instead of an extended select. There are some other problems with the extended select
also, such as the previous selections remain after you requery the listbox, even if there
is no (new) data where the selection bar is.
 
Back
Top