Selected property in multiselect listbox

G

Guest

I have a multiselect box in Access 2K. I am attempting to conditionally
select multiple items in the listbox according to the following code:

For i = 1 To (Forms!SelectionResults!Contacts_Subform.Form!List0.ListCount-1)
lngValue = Forms!SelectionResults!Contacts_Subform.Form!List0.Column(1,i)
If lngValue = lngCo Then
Forms!SelectionResults!Contacts_Subform.Form!List0.Selected(i) =
True
Else
Forms!SelectionResults!Contacts_Subform.Form!List0.Selected(i) =
False
End If
Next i

I have stepped the code through, and the program flows as expected, that is
to say, when the "lngValue" is equal to the previously set "lngCo", the
If...Else...Endif flows properly. However, when the code finishes, only the
last item found in List0 appears selected. It's as if the a user were
selecting items in the list one at a time, and not holding down the "Ctrl"
key... So, the question is, how do I programatically "hold down the Ctrl
key" for a multiselect list?
 
D

Dirk Goldgar

Todd Lemen said:
I have a multiselect box in Access 2K. I am attempting to
conditionally select multiple items in the listbox according to the
following code:

For i = 1 To
(Forms!SelectionResults!Contacts_Subform.Form!List0.ListCount-1)
lngValue =

Forms!SelectionResults!Contacts_Subform.Form!List0.Column(1,i) If
lngValue = lngCo Then
Forms!SelectionResults!Contacts_Subform.Form!List0.Selected(i) =
True
Else

Forms!SelectionResults!Contacts_Subform.Form!List0.Selected(i) = False
End If
Next i

I have stepped the code through, and the program flows as expected,
that is to say, when the "lngValue" is equal to the previously set
"lngCo", the If...Else...Endif flows properly. However, when the
code finishes, only the last item found in List0 appears selected.
It's as if the a user were selecting items in the list one at a time,
and not holding down the "Ctrl" key... So, the question is, how do I
programatically "hold down the Ctrl key" for a multiselect list?

Are you sure that the list box's MultiSelect property is set to Simple
or Extended, not None? The following simplified adaptation of your code
on a test form worked fine for me, provided that the list box was
actually set for multiselect:

Private Sub Command4_Click()

Dim i As Integer

With Me.lstEquipment

For i = 0 To (.ListCount - 1)
If CLng(.Column(0, i)) <= 33 Then
.Selected(i) = True
Else
.Selected(i) = False
End If
Next i

End With

End Sub

Does your list box have its ColumnHeads property set to True, by the
way? I notice that you are looping from 1, but the first data row is
actually 0 unless ColumnHeads is True.
 

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