Populate a list box from a Multi Select List box

G

Guest

I am trying to populate a list box with the selections I've made in another
list box on the same form using the below code. Please help my code is not
working

thanks Alex


With Me!lstfiles
For Each varItem In .ItemsSelected
strlist = strlist & .Column(1, varItem) & vbCrLf
Next varItem
Me!lstSelected.RowSource = strlist
End With
 
D

Dirk Goldgar

Alex said:
I am trying to populate a list box with the selections I've made in
another list box on the same form using the below code. Please help
my code is not working

thanks Alex


With Me!lstfiles
For Each varItem In .ItemsSelected
strlist = strlist & .Column(1, varItem) & vbCrLf
Next varItem
Me!lstSelected.RowSource = strlist
End With
Replace:

strlist = strlist & .Column(1, varItem) & vbCrLf

with:

strlist = strlist & ";" & .Column(1, varItem)

Replace:
Me!lstSelected.RowSource = strlist

with:

Me!lstSelected.RowSource = Mid(strlist, 2)
 
G

Guest

Dirk, unfortunately this did't work is there a particular reference library
this code needs?
 
D

Dirk Goldgar

Alex said:
Dirk, unfortunately this did't work is there a particular reference
library this code needs?


No special library reference should be necessary. What is the exact
code you tried, and in what way did it not work? Error message?
Mangled list? Empty rowsource list?

Did you choose the right column from the list box to add to the
rowsource? In VBA code, columns are numbered from 0.

Do the values you're selecting contain semicolons or quotes?
 
G

Guest

This is the exact code I tried is below, I get the error message
"method 'item' of object 'forms' failed"
also each of the values i'm selecting contain a period (.)

Public Sub alex()
Dim lstbox As Variant
lstbox = Forms![frmUpload]![lstfiles]
With lstbox
For Each varItem In .ItemsSelected
strlist = strlist & ";" & .Column(1, varItem)
Next varItem
Forms![frmUpload]![lstSelected].RowSource = Mid(strlist, 2)
End With
End Sub
 
D

Dirk Goldgar

Alex said:
This is the exact code I tried is below, I get the error message
"method 'item' of object 'forms' failed"
also each of the values i'm selecting contain a period (.)

Public Sub alex()
Dim lstbox As Variant
lstbox = Forms![frmUpload]![lstfiles]
With lstbox
For Each varItem In .ItemsSelected
strlist = strlist & ";" & .Column(1, varItem)
Next varItem
Forms![frmUpload]![lstSelected].RowSource = Mid(strlist, 2)
End With
End Sub

This statement is wrong:
lstbox = Forms![frmUpload]![lstfiles]

That would set lstbox to the value, if any, of the list box
Forms![frmUpload]![lstfiles]. I don't know if that's the source of the
specific error you're getting or not, but try this:

'----- start of revised code -----
Public Sub alex()

Dim strlist As String

With Forms![frmUpload]![lstfiles]
For Each varItem In .ItemsSelected
strlist = strlist & ";" & .Column(1, varItem)
Next varItem
End With

Forms![frmUpload]![lstSelected].RowSource = Mid(strlist, 2)

End Sub
'----- end of revised code -----

I notice you also didn't have a Dim statement for strlist. That
suggests, though it doesn't prove, that you don't have the VB Editor
option "Require Variable Declaration" checked. You should -- requiring
all variables to be declared will save you no end of grief in the long
run.
 
G

Guest

Thanks for your help, I got it to work

Dirk Goldgar said:
Alex said:
This is the exact code I tried is below, I get the error message
"method 'item' of object 'forms' failed"
also each of the values i'm selecting contain a period (.)

Public Sub alex()
Dim lstbox As Variant
lstbox = Forms![frmUpload]![lstfiles]
With lstbox
For Each varItem In .ItemsSelected
strlist = strlist & ";" & .Column(1, varItem)
Next varItem
Forms![frmUpload]![lstSelected].RowSource = Mid(strlist, 2)
End With
End Sub

This statement is wrong:
lstbox = Forms![frmUpload]![lstfiles]

That would set lstbox to the value, if any, of the list box
Forms![frmUpload]![lstfiles]. I don't know if that's the source of the
specific error you're getting or not, but try this:

'----- start of revised code -----
Public Sub alex()

Dim strlist As String

With Forms![frmUpload]![lstfiles]
For Each varItem In .ItemsSelected
strlist = strlist & ";" & .Column(1, varItem)
Next varItem
End With

Forms![frmUpload]![lstSelected].RowSource = Mid(strlist, 2)

End Sub
'----- end of revised code -----

I notice you also didn't have a Dim statement for strlist. That
suggests, though it doesn't prove, that you don't have the VB Editor
option "Require Variable Declaration" checked. You should -- requiring
all variables to be declared will save you no end of grief in the long
run.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

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

Similar Threads


Top