Listbox questions

T

Tom Thompson

All;

I have two questions regarding list boxes.

1) A user selects multiple items in a list box, them clicks a command
button that performs some action and the items in the list box stay
selected, how can I programmatically deselect the items in the list box
without removing them??

2) How do I extract data from a list box?? I am able to get the listindex
of a selected item in a listbox, however what I want to do is add each item
in the listbox to an array. I can't seem to get the actual data out of the
listbox.


... suggestions ???


Thanks!
 
W

Wayne Morgan

To deselect all items you need to loop through each row and set it a
deselected.

Example:
Dim I As Integer
If lstFamilyPosition.ListCount = 0 Then Exit Sub
For I = 0 To lstFamilyPosition.ListCount - 1
lstFamilyPosition.Selected(I) = False
Next I

As for getting the values, here is an example that fills a table with the
values.

Dim rst As DAO.Recordset, db As DAO.Database, varItem As Variant
Set rst = db.OpenRecordset("tblTempBirthdayLabels", dbOpenDynaset)
For Each varItem In lstFamilyPosition.ItemsSelected
With rst
.AddNew
![Family Position ID] = lstFamilyPosition.ItemData(varItem)
.Update
End With
Next varItem

The For..Each..Next loop is what extracts the values. Replace the ![Family
Position ID] with the array element and skip the recordset. If you need data
from a different column, you would need to use the listbox's column property
to specify the column. You could also loop through as in the first example,
checking the selected status of each entry.
 
T

Tom Thompson

Thanks Wayne,

This helped alot. I now have a fully functioning set of List boxes

Wayne Morgan said:
To deselect all items you need to loop through each row and set it a
deselected.

Example:
Dim I As Integer
If lstFamilyPosition.ListCount = 0 Then Exit Sub
For I = 0 To lstFamilyPosition.ListCount - 1
lstFamilyPosition.Selected(I) = False
Next I

As for getting the values, here is an example that fills a table with the
values.

Dim rst As DAO.Recordset, db As DAO.Database, varItem As Variant
Set rst = db.OpenRecordset("tblTempBirthdayLabels", dbOpenDynaset)
For Each varItem In lstFamilyPosition.ItemsSelected
With rst
.AddNew
![Family Position ID] = lstFamilyPosition.ItemData(varItem)
.Update
End With
Next varItem

The For..Each..Next loop is what extracts the values. Replace the ![Family
Position ID] with the array element and skip the recordset. If you need data
from a different column, you would need to use the listbox's column property
to specify the column. You could also loop through as in the first example,
checking the selected status of each entry.

--
Wayne Morgan
Microsoft Access MVP


Tom Thompson said:
All;

I have two questions regarding list boxes.

1) A user selects multiple items in a list box, them clicks a command
button that performs some action and the items in the list box stay
selected, how can I programmatically deselect the items in the list box
without removing them??

2) How do I extract data from a list box?? I am able to get the listindex
of a selected item in a listbox, however what I want to do is add each item
in the listbox to an array. I can't seem to get the actual data out of the
listbox.


.. suggestions ???


Thanks!
 

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