Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2013
    Posts
    9

    Unanswered: Moving selections from a combo box to another

    Moving selections in one combo box to another - help!

    --------------------------------------------------------------------------Hi,

    I have exhausted my search efforts. Here is the background:

    List 1 name: lstOpCard
    List 2 name: lstPassdown
    button name: Move

    I want the user to make their selections in List 1, click a button, and have the selections made in list 1 transfer to list 2. If there is already information in list 2, I do not want the new selections in list 1 to replace what is already in list 2. I would like it to supplement what is already in list 2.

    I currently have a button that will produce the values selected in the first box into a string (separated by commas). The string is called sTemp.

    Any help would be greatly appreciated!

    Thank you

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    If the RowSource of the second list (lstPassdown) also is a csv string, the easiest way consists in concatening it whith the csv string obtained from the selected values in the first list (lstOpCard):
    Code:
    Private Sub Move_Click()
    
        Dim var As Variant
        Dim str As String
    
        For Each var In Me.lstOpCard.ItemsSelected
            str = str & ";" & Me.lstOpCard.ItemData(var)
        Next var
        If Len(str) > 0 Then Me.lstPassdown.RowSource = Me.lstPassdown.RowSource & str
    
    End Sub
    If the elements of the second list must be sorted, here's a solution:
    Code:
    Private Sub Move_Click()
    
        Dim var As Variant
        Dim str As String
    
        For Each var In Me.lstOpCard.ItemsSelected
            str = str & ";" & Me.lstOpCard.ItemData(var)
        Next var
        If Len(str) > 0 Then Me.lstPassdown.RowSource = SortList(Me.lstPassdown.RowSource & str)
    
    End Sub
    
    Function SortList(ByVal List As String) As String
        
        Dim var As Variant
        
        var = Split(List, ";")
        QuickSort var, 0, UBound(var)
        SortList = Join(var, ";")
        
    End Function
    
    Sub QuickSort(Arr, Lower As Long, Upper As Long)
    
        Dim varPivot As Variant
        Dim varTmp As Variant
        Dim lngLower As Long
        Dim lngUpper As Long
        
        lngLower = Lower
        lngUpper = Upper
        varPivot = Arr((Lower + Upper) \ 2)
        Do While lngLower <= lngUpper
          Do While Arr(lngLower) < varPivot And lngLower < Upper
            lngLower = lngLower + 1
          Loop
          Do While varPivot < Arr(lngUpper) And lngUpper > Lower
            lngUpper = lngUpper - 1
          Loop
          If lngLower <= lngUpper Then
            varTmp = Arr(lngLower)
            Arr(lngLower) = Arr(lngUpper)
            Arr(lngUpper) = varTmp
            lngLower = lngLower + 1
            lngUpper = lngUpper - 1
          End If
        Loop
        If Lower < lngUpper Then QuickSort Arr, Lower, lngUpper
        If lngLower < Upper Then QuickSort Arr, lngLower, Upper
      
    End Sub
    Note: You'll perhaps need to replace ";" by "," as the list separator. It depends on the settings in the Control Pannel of Windows (Europe here then ";").
    Have a nice day!

  3. #3
    Join Date
    Sep 2013
    Posts
    9
    How do I make the rowsource to the combobox "lstPassdown" a csv string?

    Thanks!

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Like this:
    Code:
    Sub SaveRowSource(Optional ByVal Path As String)
    
        Dim intHandle As Integer
        Dim strPath As String
        
        If Len(Path) > 0 Then
            If Right(Path, 1) <> "\" Then Path = Path & "\"
        End If
        strPath = Path & "lstPassdown.csv"
        intHandle = FreeFile
        Open strPath For Output As #intHandle
        Print #intHandle, Me.lstPassdown.RowSource
        Close #intHandle
        
    End Sub
    And, before you ask, you can reload the list from the csv file, like this:
    Code:
    Sub LoadRowSource(Optional ByVal Path As String)
    
        Dim intHandle As Integer
        Dim strPath As String
        Dim strLine As String
        
        If Len(Path) > 0 Then
            If Right(Path, 1) <> "\" Then Path = Path & "\"
        End If
        strPath = Path & "lstPassdown.csv"
        intHandle = FreeFile
        Open strPath For Input As #intHandle
        Line Input #intHandle, strLine
        Me.lstPassdown.RowSource = strLine
        Close #intHandle
    
    End Sub
    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •