Set value of a multi-select listbox

G

Guest

Anybody have any ideas how to "select" an item in a multi-select listbox
through code in Access 2000?

I have a listbox that displays a bunch of items, in a particular sort order.
I have buttons to the side of the listbox to move the selected item up/down
the list. The first thing I do is get the value from the bound column of the
currently selected item (I tried to do this without the reference to the
column, but the list itself had a value of null). I then pass a value to a
subroutine which updates the sequence number of the selected item and the one
that is above or below it in the list (executes a couple of sql commands to
update the underlying data table). I then requery the listbox and try to set
the value of the list back to its original value so I can just hit the
up/down arrows again.

CurrentID = me.lst_myList.column(0)
Call ChangeListSequence(-1)
me.lst_myList.requery
me.lst_myList.value = CurrentID

The problem is that when I try to set the value of the list, the does not
seem to work. If I change the multi-select property of the list to None (0),
this works, but I need the multi-select aspect of the list for other
purposes. I even tried changing the multi-select property in code, but it
gives me an error. I even looped through all the items in the list and
turned the selected property off for all but the item I was interested in,
but although this visually selected the item, the next time I clicked on the
up/down arrow, the line that references the value of the column returned a
NULL value and errored out.
 
G

Graham Mandeno

Hi Dale

The Value property of a multi-select list is meaningless, because zero or
multiple rows can be selected.

The only meaningful ways to refer to or manipulate the selected items in the
list are via the Selected property and the ItemsSelected collection.

It seems from your description that you want to select only one value at a
time anyway. So why is the listbox MultiSelect?
 
D

Dirk Goldgar

Dale Fye said:
Anybody have any ideas how to "select" an item in a multi-select
listbox through code in Access 2000?

I have a listbox that displays a bunch of items, in a particular sort
order. I have buttons to the side of the listbox to move the
selected item up/down the list. The first thing I do is get the
value from the bound column of the currently selected item (I tried
to do this without the reference to the column, but the list itself
had a value of null). I then pass a value to a subroutine which
updates the sequence number of the selected item and the one that is
above or below it in the list (executes a couple of sql commands to
update the underlying data table). I then requery the listbox and
try to set the value of the list back to its original value so I can
just hit the up/down arrows again.

CurrentID = me.lst_myList.column(0)
Call ChangeListSequence(-1)
me.lst_myList.requery
me.lst_myList.value = CurrentID

The problem is that when I try to set the value of the list, the does
not seem to work. If I change the multi-select property of the list
to None (0), this works, but I need the multi-select aspect of the
list for other purposes. I even tried changing the multi-select
property in code, but it gives me an error. I even looped through
all the items in the list and turned the selected property off for
all but the item I was interested in, but although this visually
selected the item, the next time I clicked on the up/down arrow, the
line that references the value of the column returned a NULL value
and errored out.

Unlike a single-select list box, a multi-select list box has no value --
that's why you can't set it. What you need to do is loop through the
rows of the list box, find the row (or rows) that has the values you
want to select, and then set that row's Selected property.

I'm not sure that your line
CurrentID = me.lst_myList.column(0)

is reliably going to give you the ID you want, because you aren't
specifying what row you want it from. I suspect it's going to give you
the value from the row that is currrently pointed to by the combo's
ListIndex, but that may or may not be the any of the rows that is
currently selected. You'd probably do better to do something like this:

' *** AIR CODE ***
With Me.lst_myList
If .ItemsSelected.Count > 0 Then
CurrentID = .Column(0, .ItemsSelected(0))
End If
End With

Note that the above is really only designed to pick up the ID of one
selected item.

Then your code to reselect that item after requerying would look
something like this:

' *** AIR CODE AGAIN ***
Dim lngI As Long

With Me.lst_myList
For lngI = Abs(.ColumnHeads) To (.ListCount - 1)
If .ItemData(lngI ) = CurrentID Then
.Selected(lngI ) = True
Exit For
End If
Next lngI
End With
 
G

Guest

GRaham,

The listbox is used for several purposes: 1)assigning/removing items to/from
a particular group, 2) sorting the items that are assigned to a group.

For part 1 of this process, I use the multiselect to enable
assigning/removing multiple items at one time. I tried setting it up so that
if only one item is selected, and the user selects the Up or Down button, it
would change the property of the box, but apparantly the multiselect property
is not available at run-time. I guess I'll just have to write a short
function to return the value of the only selected item. No big deal, but
wanted to check if I was missing something.

Thanks for your feedback.


Graham Mandeno said:
Hi Dale

The Value property of a multi-select list is meaningless, because zero or
multiple rows can be selected.

The only meaningful ways to refer to or manipulate the selected items in the
list are via the Selected property and the ItemsSelected collection.

It seems from your description that you want to select only one value at a
time anyway. So why is the listbox MultiSelect?
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Dale Fye said:
Anybody have any ideas how to "select" an item in a multi-select listbox
through code in Access 2000?

I have a listbox that displays a bunch of items, in a particular sort
order.
I have buttons to the side of the listbox to move the selected item
up/down
the list. The first thing I do is get the value from the bound column of
the
currently selected item (I tried to do this without the reference to the
column, but the list itself had a value of null). I then pass a value to
a
subroutine which updates the sequence number of the selected item and the
one
that is above or below it in the list (executes a couple of sql commands
to
update the underlying data table). I then requery the listbox and try to
set
the value of the list back to its original value so I can just hit the
up/down arrows again.

CurrentID = me.lst_myList.column(0)
Call ChangeListSequence(-1)
me.lst_myList.requery
me.lst_myList.value = CurrentID

The problem is that when I try to set the value of the list, the does not
seem to work. If I change the multi-select property of the list to None
(0),
this works, but I need the multi-select aspect of the list for other
purposes. I even tried changing the multi-select property in code, but it
gives me an error. I even looped through all the items in the list and
turned the selected property off for all but the item I was interested in,
but although this visually selected the item, the next time I clicked on
the
up/down arrow, the line that references the value of the column returned a
NULL value and errored out.
 
D

Dale Fye

Thanks, Dirk.

I came up with basically the same code after after I got Graham's reply.
Dale
 

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