Results 1 to 3 of 3

Thread: [RESOLVED] MultiSelect ListBox To Populate Another.

  1. #1

    Thread Starter
    Addicted Member skea's Avatar
    Join Date
    Mar 2006
    Posts
    187

    Resolved [RESOLVED] MultiSelect ListBox To Populate Another.

    Hello Folks, i need some help here.
    I have a listbox(call it lst2) that i populate from an array. From it i can populate another listbox(lst3) with items using a related field.
    My problem is that, i am trying to use selecteditem.count but when i select one item(i dont know how many they are since i fill my lst2 from an array), my lst3 is not populated and yet when i remove my function from the if statement, it works fine. Any Idea.
    Below is my code.

    VB Code:
    1. Private Sub lst2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lst2.SelectedIndexChanged
    2.         Me.lst3.BackColor = Color.White
    3.         Me.lst3.Enabled = True
    4.         If Me.lst3.SelectedItems.Count = 1 Then
    5.             PopulateCountyList()
    6.         Else
    7.         End If
    8.     End Sub

    VB Code:
    1. Sub PopulateCountyList()
    2.         Dim strSQL As String = "SELECT CtyID,County FROM Counties WHERE(DistID=" & CType(Me.lst2.Items(Me.lst2.SelectedIndex), ListItem).ID & ") ORDER BY County ASC"
    3.         Me.lst3.Items.Clear()
    4.         PopulateColumnList(Me.lst3, strSQL, strAccessConn, "County", "CtyID")
    5.     End Sub

  2. #2

    Thread Starter
    Addicted Member skea's Avatar
    Join Date
    Mar 2006
    Posts
    187

    Re: MultiSelect ListBox To Populate Another.

    Ok. The Above may be complicated but i have this simple one. I want to get a list of selected items in my multisimple listbox(this time not populated with an array).
    Below i code i am trying to implement, extracted from one of my access dbs.
    The problem i am facing is that, when i select more than one, the selected item on top is displayed as selected too.
    Here is my Code
    VB Code:
    1. Sub PopulateCountyList2()
    2.         Dim ctrl As ListBox = Me.lst2
    3.         Dim v As Object
    4.         'Dim i As Integer
    5.         With ctrl
    6.             If ctrl.SelectedItems.Count = 0 Then
    7.                 Me.lst3.Enabled = False
    8.                 Me.lst3.Items.Clear()
    9.                 Me.lst3.BackColor = System.Drawing.SystemColors.Control
    10.             Else
    11.                 Me.lst3.Enabled = True
    12.                 Me.lst3.BackColor = System.Drawing.SystemColors.Window
    13.                 'Dim strSQL As String = "SELECT CtyID,County FROM Counties WHERE(DistID=" & CType(Me.lst2.Items(Me.lst2.SelectedIndex), ListItem).ID & ") ORDER BY County ASC"
    14.                 Me.lst3.Items.Clear()
    15.                 Dim strSQL As String = "SELECT DISTINCT County FROM qryDistCtySubCty WHERE District IN ("
    16.                 For Each v In .SelectedItems
    17.                     strSQL = strSQL & "'" & .SelectedItem() & "',"
    18.                 Next
    19.                 strSQL = strRight(strSQL, 1) & ")"
    20.                 MsgBox(strSQL)
    21.                 PopulateSingleList(Me.lst3, strSQL, "County")
    22.             End If
    23.         End With
    24.        
    25.     End Sub

    Here is the VBA code
    Code:
    Private Sub lst0_AfterUpdate() 'WatchOut:Appends the IDPCamps to previous list in IDPCamp Listbox
    Dim ctrl As Control, v As Variant
    Dim db As DAO.Database, qdf As DAO.QueryDef, i As Integer
    Dim strSQL As String
    Set ctrl = Me.lst0
    Set db = CurrentDb()
    With ctrl
      If .ItemsSelected.Count = 0 Then
      Me.lst2.Enabled = False
      Else
      Me.lst2.Enabled = True
      strSQL = "SELECT IDPCampName FROM QryDistCamps WHERE District IN ("
      For Each v In .ItemsSelected
      strSQL = strSQL & "'" & .Column(0, v) & "',"
      Next
      strSQL = Left(strSQL, Len(strSQL) - 1) & ")"
     End If
    On Error Resume Next
    db.QueryDefs.Delete "QryDistCamps2"
    Set qdf = db.CreateQueryDef("QryDistCamps2", strSQL)
    Me.lst2.RowSource = "QryDistCamps2"
    
     For i = 0 To .ListCount - 1
     If .Selected(i) Then
       If .Column(0, i) = "All" Then
           Me.lst2.RowSource = "SELECT IDPCampName FROM QryDistCamps"
       End If
     End If
     Next i
     
    End With
    qdf.Close
    db.Close
    Set qdf = Nothing
    Set db = Nothing
    Call PopulateTBDistricts
    End Sub
    Below are the Images, of the message which i expect
    SELECT DISTINCT County FROM qryDistCtySubCty WHERE District IN ('Apac','Gulu') and the form which i choose from.
    Attached Images Attached Images  
    Last edited by skea; May 3rd, 2006 at 08:03 AM.

  3. #3

    Thread Starter
    Addicted Member skea's Avatar
    Join Date
    Mar 2006
    Posts
    187

    Re: MultiSelect ListBox To Populate Another.

    Yes....I got it, how simple it was.

    strSQL += & "'" & .SelectedItems.Item(i) & "',"

Posting Permissions

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



Click Here to Expand Forum to Full Width