Break between ItemSelected from List Box

P

Pamela

I have a text box that takes the entries from a multi-select list box and
concatenates them. My code is not reading that there is supposed to be a
comma and space between each of the selected items from my list box. My code
currently lists them correctly but all together but running into each other
with no spaces. I believe the problem lies in line 5 where "strText" is. We
tried setting it to varRow but that just lists the # of items selected. Here
is my code:
Dim varRow As Variant, strText As String
'strText = vbNullString
For Each varRow In Me.lbDamagedParts.ItemsSelected
strText = strText & Me.lbDamagedParts.Column(0, varRow)
Me.Text39 = "The vehicle sustained damage to the " & strText & ", "
Next varRow
Me.Text39 = Left(Me.Text39, Len(Me.Text39) - 1) & "."

Thank you so much!

Pamela
 
D

Douglas J. Steele

Dim varRow As Variant, strText As String

strText = vbNullString
If Me.lbDamagedParts.ItemsSelected.Count > 0 Then
For Each varRow In Me.lbDamagedParts.ItemsSelected
strText = strText & Me.lbDamagedParts.Column(0, varRow) & ", "
Next varRow
Me.Text39 = "The vehicle sustained damage to the " & _
Left(strText, Len(strText) - 2) & "."
Else
Me.Text39 = "The vehicle did not sustain any damage."
End If
 
T

Tom Wickerath

P

Pamela

Thank you, Thank you, Thank you! I've been struggling with this aspect for 3
days and Poof! it's fixed - and working beautifully! I really appreciate
your time and help in this and all of those out there that have spent time
helping me on this project!

Pamela
 
P

Pamela

Hi Doug,

I was so excited when you gave me this code and it worked just perfectly. I
decided to try to make the form more user-friendly and filtered the listbox
items by another cbo on the form. I thought I had it nailed today but when I
tested it, all of a sudden, the items selected aren't showing up from your
code here. Instead, I am getting, "The vehicle sustained damage to the , , ,
, ,." Any ideas why the code that worked so beautifully before would stop
reading the variable just because the box is now filtered? And note, that it
isn't taking the "Else" case should there be no selection, so it's reading
that there is a selection and even reading the correct # of commas - but not
the text itself. Thank you so very much for any help you can give me on
this!!

Pamela
 
D

Douglas J. Steele

Did you change what order the fields are in the list box? Remember that the
code is supposed to be grabbing whatever's in the first column of each of
the selected rows.
 
P

Pamela

When staring at your code, I remembered something I had read about the Column
function which triggered the possibility that maybe the column count had
changed. I added back the ID to the list box query and it worked again.
Thanks so much!
 

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