none
ListBox.ItemSelected() is always zero RRS feed

  • Question

  • Hello< 

    I am running Access 2007 - 2016. I have a for with a listbox. There is code to search instantly as you type which is working great. When the user makes a single selection I want to populate the rest of the form. In the debugger I can set the OnClick event fire and the List Count is correct, but no item is selected. Here's some test code that handles the OnClick event, is there some setting or something I'm missing?

    Private Sub lstItems_Click()
      Dim ctlList As Control, varItem As Variant
     
      Dim frm As Form, ctl As Control
      Dim varItm As Variant
     
      I As Integer

      For I = 0 To Me.lstItems.ListCount - 1
      If Me.lstItems.Selected(I) Then
      MsgBox ("Item " & I & " Selected")
    End If
    Next I
    End Sub


    Friday, September 13, 2019 7:38 PM

All replies

  • The "I As Integer" is a typo from cutting and pasting, should be "Dim I As Integer"
    Friday, September 13, 2019 8:18 PM
  • Use something like this:

        Dim Items   As Variant
        Dim Item    As Variant
        
        Items = Null
        For Each Item In Me!lstItems.ItemsSelected
            Items = (Items + ", ") & Me!lstItems.ItemData(Item)
        Next
        MsgBox Nz(Items)
        
    


    Gustav Brock

    Saturday, September 14, 2019 8:03 AM
  • Hi Mark,

    To add further, make sure you have the property "Multi Select" set to "Extended".

    And your code should be something like

    For i = 0 To Me.lstItems.ListCount - 1
       If Me.lstItems.Selected(i) Then
       MsgBox ("Item " & Me.lstItems.ItemData(i) & " Selected")
     End If
     Next I

    Monday, September 16, 2019 4:12 AM
  • HI

    Just use the After Update  event instead of the click event.

    and then you can use your original code .

    Asaf

    Monday, September 16, 2019 7:49 PM