multiple selection list box

  • Follow


The following code will allow multiple selections from a list box to be fed 
into a list box.  My question is how would I modity this code so that I could 
use it with multiple list boxes on the same form.  It works for only one box. 
 Can, for example, multiple instances of the code with object names changed, 
be attached to each list box instead of the form?
Option Compare Database
Option Explicit

Private Sub Form_Current()
    Dim oItem As Variant
    Dim bFound As Boolean
    Dim sTemp As String
    Dim sValue As String
    Dim sChar As String
    Dim iCount As Integer
    Dim iListItemsCount As Integer
    
    sTemp = Nz(Me!mySelections.Value, " ")
    iListItemsCount = 0
    bFound = False
    iCount = 0

    Call clearListBox
        
    For iCount = 1 To Len(sTemp) + 1
    sChar = Mid(sTemp, iCount, 1)
        If StrComp(sChar, ",") = 0 Or iCount = Len(sTemp) + 1 Then
            bFound = False
            Do
                If StrComp(Trim(Me!NamesList.ItemData(iListItemsCount)), 
Trim(sValue)) = 0 Then
                    Me!NamesList.Selected(iListItemsCount) = True
                    bFound = True
                End If
                iListItemsCount = iListItemsCount + 1
            Loop Until bFound = True Or iListItemsCount = 
Me!NamesList.ListCount
            sValue = ""
        Else
            sValue = sValue & sChar
        End If
    Next iCount
End Sub
    
Private Sub clearListBox()
    Dim iCount As Integer
        
    For iCount = 0 To Me!NamesList.ListCount
        Me!NamesList.Selected(iCount) = False
    Next iCount
End Sub

Private Sub testmultiselect_Click()
    Dim oItem As Variant
    Dim sTemp As String
    Dim iCount As Integer
    
    iCount = 0
            
    If Me!NamesList.ItemsSelected.Count <> 0 Then
        For Each oItem In Me!NamesList.ItemsSelected
            If iCount = 0 Then
                sTemp = sTemp & Me!NamesList.ItemData(oItem)
                iCount = iCount + 1
            Else
                sTemp = sTemp & "," & Me!NamesList.ItemData(oItem)
                iCount = iCount + 1
            End If
        Next oItem
    Else
        MsgBox "Nothing was selected from the list", vbInformation
        Exit Sub  'Nothing was selected
    End If
    
    Me!mySelections.Value = sTemp
End Sub
    
Private Sub clrList_Click()
    Call clearListBox
    Me!mySelections.Value = Null
End Sub



-- 
stan
0
Reply Utf 1/9/2010 8:12:01 PM


0 Replies
525 Views

(page loaded in 0.049 seconds)

Similiar Articles:











7/30/2012 4:04:45 PM


Reply: