Public function to get selected list items

  • Thread starter Thread starter BobRoyAce
  • Start date Start date
B

BobRoyAce

I have a List Box on one form (say, FormA) and the user can multiselect
items in it. At some point, I minimize FormA and load a second form (say,
FormB). I know how to determine the selected items of a List Box in general.
However, my question is how can I create a general function in a module that
can return the selected items from a ListBox passed to it. I tried the
following:

Created a function in Module1:

Public Function GetSelectedItemsInListBox(lst As ListBox) As String
Dim varItm As Variant
Dim sLines As String

For Each varItm In lst.ItemsSelected
sLines = sLines & lst.Column(0, varItm) & ","
Next varItm

If Len(sLines) > 0 Then
' Get rid of final comma at end of string
sLines = Left$(sLines, Len(sLines) - 1)
End If

GetSelectedItemsInListBox = sLines
End Function

Then, in FormB, had code as follows:

Dim sSelectedLines As Integer

sSelectedLines = GetSelectedItemsInListBox([Forms]![FormA]!lstLineNames)

When the above line is executed, I get an error stating that there is a Type
mismatch. What could be causing this?
 
-----Original Message-----
I have a List Box on one form (say, FormA) and the user can multiselect
items in it. At some point, I minimize FormA and load a second form (say,
FormB). I know how to determine the selected items of a List Box in general.
However, my question is how can I create a general function in a module that
can return the selected items from a ListBox passed to it. I tried the
following:

Created a function in Module1:

Public Function GetSelectedItemsInListBox(lst As ListBox) As String
Dim varItm As Variant
Dim sLines As String

For Each varItm In lst.ItemsSelected
sLines = sLines & lst.Column(0, varItm) & ","
Next varItm

If Len(sLines) > 0 Then
' Get rid of final comma at end of string
sLines = Left$(sLines, Len(sLines) - 1)
End If

GetSelectedItemsInListBox = sLines
End Function

Then, in FormB, had code as follows:

Dim sSelectedLines As Integer

sSelectedLines should be declared as type String
sSelectedLines = GetSelectedItemsInListBox([Forms]![FormA]!lstLineNames)

When the above line is executed, I get an error stating that there is a Type
mismatch. What could be causing this?


.

Hope That Helps
Gerald Stanley MCSD
 
Back
Top