Using list box .selected = true does not appear to select the item

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a list box with a list of names, each with an ID in column 0.

I select a name and click on a button, which sends me off to a subform that
allows the name to be edited, etc. When I return to the list, I requery the
list and I want to have the same item that was selected when I left to be
selected again. I save the NameID before going to the subform to do this.
The code to reselect the item looks like this:

For i = 0 to MyList.ListCount - 1
If MyList.column(0,i) = NameID then MyList.selected(i) = True
next i

When I see the list, the item in question is indeed highlighted, but there
is a dotted box around the first item in the list.

If I then try to extract information about the selected item using
msgbox MyList.column(0)
I get an error that says "Invalid use of Null", which means it is not
actually addressing the highlighted record. If I then click on the record,
then everything works fine.

I have multiselect set to Extended if that makes any difference.

Can someone explain what is going on here and the best way to get around it?
Thanks...
 
It's because of MultiSelect.

You can't simply refer to the control when it's on. You have to look at the
ItemsSelected collection:

Dim varItem As Variant

For Each varItem In MyList.ItemsSelected
MsgBox MyList.Column(0, varItem)
End If
 
Back
Top