Page 1 of 2 12 LastLast
Results 1 to 15 of 30
  1. #1
    Deepak.Doddagoudar is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Mar 2018
    Location
    Sweden
    Posts
    43

    Multiselect Multiple Listboxes

    Hello Everyone,


    I have 4 list boxes : Machine, Machine System, Machine SubSystem and Components with multiple selection enabled to "Simple" for all. Each list box is hierarchically related, with MAchine on top and Components at the bottom. I also have a button to select for each listbox. Everything is working fine for me. The value I select in Machine gives me the values in the Machine System and the values I select in the MAchine System gives me the value for Machine Sub Subsytem and so on. The row source for the Machine listbox is Machine Table and the row sources for the other list boxes is the Value List.The problem is without selecting the values in the MAchine listbox all values in the other listboxes are getting displayed after clicking their respective select button. How do I eliminate this?
    I am new also new to VBA. Please Help.

    Regards
    Deepak


  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    I suggest you review Cascading comboboxes.
    There also several youtube videos on this subject.

  3. #3
    Deepak.Doddagoudar is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Mar 2018
    Location
    Sweden
    Posts
    43
    Thank you for the reply but that is a combo box with no multiple selection. In my case the from contains list boxes with all "multiselection" enabled.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Please give us the requirement that needs cascading Listboxes multiselect .
    I'm not saying it doesn't apply, but most situations involve cascading combos.

    Here is a link that you may find interesting.

  5. #5
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by Deepak.Doddagoudar View Post
    The row source for the Machine listbox is Machine Table and the row sources for the other list boxes is the Value List.
    Something seems strange to me that the other listboxes row sources would be value lists, but I don't know your requirements.

    The problem is without selecting the values in the MAchine listbox all values in the other listboxes are getting displayed after clicking their respective select button. How do I eliminate this?
    What is are the respective select button's supposed to do? Are they supposed to filter the rows in the listboxes themselves? Or filter the data in a query?

    I assume after each click you'd need to loop through each listbox to determine what's selected and build a query filter.
    https://stackoverflow.com/questions/19551754/how-do-i-return-multi-select-listbox-values-into-a-sentence-using-word-vba

  6. #6
    Deepak.Doddagoudar is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Mar 2018
    Location
    Sweden
    Posts
    43
    Hello,
    When I open the form all four listboxes are visible. The values in the first listbox i.e Machine are visible. I select any value in it and click the button, respective related data gets selected in the second list box. This is as per my requirement. But also without selecting the values from the first Machine listbox if I click the button all its related values are displayed in the Machine system (second listbox). In short the second, third and fourth listbox is showing their all respective values without selecting any from the first listbox

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    It sounds like you may be building a dynamic value list rather than related tables to perform lookup which are the row sources of the combos.

  8. #8
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    You said the row source of listboxes 2,3 and 4 are value lists, is this correct? Listbox 1 is the only one with an underlying table?

    What code have you tried?

    When you say
    I select any value in it and click the button, respective related data gets selected in the second list box
    do you mean that listboxes 1,2,3 should only display rows related to the previous selection or they should display all rows and select related rows?

    Im assuming it goes:
    1. Make (multi)selection in listbox 1
    2. click select 1
    3. load related rows in listbox 2 with nothing selected
    4. Make (multi)selection in listbox 2
    5. click select 2
    6. load related rows in listbox 3 with nothing selected
    7. Make (multi)selection in listbox 3
    8. click select 3
    9. load related rows in listbox 4 with nothing selected
    10. Make (multi)selection in listbox 4
    11. click select 4
    12. Profit

    Correct?

    If that's the case your row source is going to need to be dynamic. Might be easier to use queries as the row sources rather than value lists.

  9. #9
    Deepak.Doddagoudar is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Mar 2018
    Location
    Sweden
    Posts
    43
    Yes that is right. Listbox 1 row source is the underlying table and 2,3 and 4 has value list row source. Let me make it simple for you, Listbox 1 records are visible. 2,3 and 4 are not visible but when I click the 2 button without selecting the values all records are visible. 2 must only display records related to selection from 1. 3 must display records from displayed records from selection in 1 and so on.
    This is the code:Private Sub btnMachine_Click()



    Dim intMachineID As Integer
    Dim strWhrMachineID As String
    Dim strSQLstr As String
    Dim rsData As Recordset

    Form_frmFILTER.listMachineSystem.RowSource = ""

    For intMachineID = 0 To Form_frmFILTER.listMachine.ListCount - 1
    If Form_frmFILTER.listMachine.Selected(intMachineID) Then
    If strWhrMachineID <> "" Then
    strWhrMachineID = strWhrMachineID & " or " & "tblMachineSystem.[MAchine ID] = " & Form_frmFILTER.listMachine.Column(0, intMachineID)
    Else
    strWhrMachineID = "Where " & "tblMachineSystem.[MAchine ID] = " & Form_frmFILTER.listMachine.Column(0, intMachineID)
    End If
    End If
    Next intMachineID

    strSQLstr = "SELECT tblMachineSystem.[Machine System ID], tblMachineSystem.[Machine System], tblMachineSystem.[MAchine ID] " & _
    "FROM tblMachineSystem " & _
    strWhrMachineID
    Set rsData = CurrentDb.OpenRecordset(strSQLstr)

    If (rsData.RecordCount <> 0) Then
    Do While Not rsData.EOF
    Form_frmFILTER.listMachineSystem.AddItem Item:=rsData.Fields(0).Value & ";" & rsData.Fields(1).Value & ";" & rsData.Fields(2).Value
    rsData.MoveNext
    Loop

    If Form_frmFILTER.listMachine.Selected(intMachineID) = 0 Then
    Me.listMachineSystem.Value = True
    Else
    Me.listMachineSystem.Value = False
    End If





    End If
    End Sub

    This is the code to filter from 1 to 2. Similar codes from 2 to 3 and 3 to 4.

  10. #10
    Deepak.Doddagoudar is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Mar 2018
    Location
    Sweden
    Posts
    43
    Thats precisely what i mean. Sorry I did not see your full message. But how to dynamically populate subsequent listbox?

  11. #11
    Deepak.Doddagoudar is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Mar 2018
    Location
    Sweden
    Posts
    43
    Yeah, that might the case but I do not have any clue how to do it. I just thought that I can write a simple code like if the items selected for list box one is true then value list for list box two is true else false. But that is not working.

  12. #12
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    I'm looking at your code and the approach seems fine to me, there must be a bug in the code somewhere. Can you run a debug.print strSQLstr and report the results? I'd suggest you but a breakpoint in the top of the button's code and step through it to watch how everything is going.

    On a side note wouldn't it be simpler to set the rowsource = strSQLstr and then requery the listbox instead of then looping through the recordset and adding each row to a value list?

  13. #13
    Deepak.Doddagoudar is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Mar 2018
    Location
    Sweden
    Posts
    43
    How do I debug it? I am quite new to access and have never debugged.

  14. #14
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Oh it just dawned on me!

    The problem is without selecting the values in the MAchine listbox all values in the other listboxes are getting displayed after clicking their respective select button. How do I eliminate this?
    That's because in your code if the loop doesn't find a selected list item you essentially no longer have a WHERE clause in your sql statement.

    Insert this line between Next intMachineID and strSQLstr = "SELECT tblMachineSystem.[Machine System ID]...

    Code:
     If strWhrMachineID = "" Then Exit Sub
    Should look like

    Code:
        For intMachineID = 0 To Form_frmFILTER.listMachine.ListCount - 1
            If Form_frmFILTER.listMachine.Selected(intMachineID) Then
                If strWhrMachineID <> "" Then
                    strWhrMachineID = strWhrMachineID & " or " & "tblMachineSystem.[MAchine ID] = " & Form_frmFILTER.listMachine.Column(0, intMachineID)
                Else
                    strWhrMachineID = "Where " & "tblMachineSystem.[MAchine ID] = " & Form_frmFILTER.listMachine.Column(0, intMachineID)
                End If
            End If
        Next intMachineID
        
        If strWhrMachineID = "" Then Exit Sub
            
        strSQLstr = "SELECT tblMachineSystem.[Machine System ID], tblMachineSystem.[Machine System], tblMachineSystem.[MAchine ID] " & _
                        "FROM tblMachineSystem " & strWhrMachineID
        Set rsData = CurrentDb.OpenRecordset(strSQLstr)
    If there are no selections in the first listbox this line will quit the sub leaving listbox 2 empty.

  15. #15
    Deepak.Doddagoudar is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Mar 2018
    Location
    Sweden
    Posts
    43
    You are an ABSOLUTE GENIUS. Thanks a ton. You are a real Absolute Performer. Cheers

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Multiple Listboxes on form with subform
    By cbrsix in forum Programming
    Replies: 12
    Last Post: 08-01-2013, 03:35 PM
  2. Multiple Listboxes in Search Form
    By cbrsix in forum Programming
    Replies: 6
    Last Post: 05-03-2013, 12:11 PM
  3. Replies: 5
    Last Post: 02-05-2013, 01:18 PM
  4. Combining data from multiple listboxes
    By dshillington in forum Access
    Replies: 1
    Last Post: 10-04-2012, 11:53 AM
  5. Multiple Listboxes
    By Butterflies88 in forum Forms
    Replies: 1
    Last Post: 02-11-2009, 10:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums