Help with multi-select listbox

Help with multi-select listbox

Post by mcowa » Wed, 20 Oct 2004 01:20:57


I am using Access 2000. I have the Developer's handbook and got the
code for the mulit select listbox from there. It sends the selected
value to another listbox, lstselected. What I need to do is loop
through there and add each item to a table. I can add the other info
fromt he form to a table, just not the otems in the listbox. It only
shows as 0. I tried using the varItem and it told me that the action
is not supported. I'm a bit rusty in my Access programming so any help
is appreciated. Thanks.

Melissa Cowan
 
 
 

Help with multi-select listbox

Post by pietlinde » Wed, 20 Oct 2004 14:13:44


there's code at mvps.org for looping through the selecteditems
collection of a listbox. Then you'd just open a recordset based on
the table and then use the .Add method to add a new record, .Edit it,
then .Update it once you've made all your additions. Nothing doing.
Should all be under "Recordset" in the help.

 
 
 

Help with multi-select listbox

Post by mcowa » Wed, 20 Oct 2004 22:29:20

Hmm, well, I've looked there and can't find anything that works. I've
found an article on passing values from a mulit-select box to a query
but nothing about how to loop through itmes in a listbox and add it to
a table. I just really need info on how to go through the items in a
listbox. I've got the code to add the records to a table and that part
works. It just won't pick up each name in the SelectedItems box and
add it. I'm assuming I need to loop through each name in that box as
the names get sent over to it from the listbox with all the Employee
names in it. Am I correct? Thinking wrong? Trying to be too complex on
my form?

Melissa
 
 
 

Help with multi-select listbox

Post by mcowa » Thu, 21 Oct 2004 02:57:49

Ok, I solved my own problem. I used the listcount and then loop
through the listbox while a number I'm incrementing is less than that.
Here is the code for what I did. If anyone has any better suggestions
than that I am always open to them.

Dim db1 As DAO.Database
Dim rs1 As DAO.Recordset
Dim ItemCount As Long
Dim Counting As Long

ItemCount = lstSelected.ListCount
Counting = 0
Set db1 = CurrentDb()
Set rs1 = db1.OpenRecordset("MultiSelectAction")

With rs1
While Counting < ItemCount
.AddNew
![CPAR_ID] = txtCPAR_ID.Value
![Employee] = lstSelected.Column(0, Counting)
![ActionNumber] = ActionID.Value
.Update
Counting = Counting + 1
Wend
End With


Thanks.
Melissa Cowan
 
 
 

Help with multi-select listbox

Post by janros » Thu, 21 Oct 2004 20:54:15

Here's what I use:

For Each i In Me![lstbox].ItemsSelected
blah = blah & Me![lstbox].ItemData(i)
Next i

HTH

Jan







Jan Stempel
Stempel Consulting
 
 
 

Help with multi-select listbox

Post by janros » Thu, 21 Oct 2004 20:55:40

Forgot to add, you have to dim the i as integer (or, I seem to have done it as
variant in my last use; not sure why).

Jan
Jan Stempel
Stempel Consulting