Selected Items in Listbox

I have a multi-select listbox which puts a tick in the checkbox if the item is selected by the user.

Eg.

Apples
Oranges (selected)
Bananas (selected)
Strawberries
Grapes (selected)

What I want to be able to do is:

a) Send the selected items to a form field (Text1) in my document in the following format:

Oranges
Bananas
Grapes

b) When my dialog box is initialized, reflect which items were selected from the information that is now in my form field (Text1), ie. Oranges, Bananas, Grapes.

I figure this probably needs to be done using an array, which I'm not very familiar with.

Thanks in advance.
lp3535Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DhaestCommented:
In which codelanguage are you programming ?

How to get multiple selected values and items from listbox ?
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim strItem As [String]
    For Each selecteditem As [Object] In listBox1.SelectedItems
        strItem = TryCast(selecteditem, [String])
            'Process(strItem);
        System.Diagnostics.Debug.WriteLine(strItem)
    Next
End Sub
0
lp3535Author Commented:
Sorry, that would have helped!  VBA - form field is in a Word doc.
0
DhaestCommented:
Dim lItem As Long
    For lItem = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(lItem) = True Then            
                 ....
        End If
    Next
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

DhaestCommented:
Sub FindSelectedItems()
' finds selected items in ListBox1 in userform TestDialog in Excel97
Dim Msg As String, i As Integer
    Msg = ""
    With TestDialog.ListBox1
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                Msg = Msg & .List(i) & Chr(13)
            End If
        Next i
    End With
    MsgBox Msg, , "Selected items in ListBox1"
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lp3535Author Commented:
Thanks Dhaest,

I've changed your code slightly (see below) to send the result to my form field, but how do I then tick the selections when I open the dialog box again?
Dim Msg As String, i As Integer
    Msg = ""
    With Me.lbRelationshipType
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                Msg = Msg & .List(i) & Chr(13)
            End If
        Next i
    End With
    
    ActiveDocument.FormFields("Text1").Result = Msg

Open in new window

0
DhaestCommented:
How is the string formatted to set the items ?

Suppose you have this:
Dim myString as string
Dim avarSplit As Variant
Dim intIndex As Integer

myString = "Oranges,Bananas"
avarSplit = Split( myString , ",")
For intIndex = LBound(avarSplit) To UBound(avarSplit)
    With Me.lbRelationshipType
        For i = 0 To .ListCount - 1
            If .Item(i) =  avarSplit(intIndex)   Then
                .Selected(i) = true
            End If
        Next i
    End With
Next
0
lp3535Author Commented:
The string is identical to what your earlier code was, so seperated by Chr(13).
0
lp3535Author Commented:
I forgot to add - that code generated an error at this line:

If .Item(i) =  avarSplit(intIndex)   Then

0
DhaestCommented:
Just tested in a small vb-application
Dim myString As String
Dim avarSplit As Variant
Dim intIndex As Integer
 
myString = "Oranges" + Chr(13) + "Bananas"
avarSplit = Split(myString, Chr(13))
For intIndex = LBound(avarSplit) To UBound(avarSplit)
    With List1
        For i = 0 To .ListCount - 1
        
            If .List(i) = avarSplit(intIndex) Then
                .Selected(i) = True
            End If
        Next i
    End With
Next

Open in new window

0
lp3535Author Commented:
Thanks Dhaest, that's perfect.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.