locked
Is it possible to get values from multiple columns while iterating over ListBox.SelectedItems? RRS feed

  • Question

  • Using a ListBox set to allow selection of multiple items, is it possible to get the value of different columns?

    When I use For Each item in Me.ListBox.ItemsSelected, item is a variant, and I don't know how to extract any data from it except one value.
    Wednesday, February 15, 2017 10:11 PM

Answers

  • You can use the Column property of the list box control for this. For example, to get the value of the 3rd column:

        Dim itm As Variant
        For Each itm In Me.List0.ItemsSelected
            Debug.Print Me.List0.Column(2, itm)
        Next itm

    where List0 is the name of the list box. The column number is zero-based, so use 0 for the first column, 1 for the second column etc.

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by HTHP Wednesday, February 15, 2017 11:17 PM
    Wednesday, February 15, 2017 10:40 PM
  • Using a ListBox set to allow selection of multiple items, is it possible to get the value of different columns?

    When I use For Each item in Me.ListBox.ItemsSelected, item is a variant, and I don't know how to extract any data from it except one value.

    Yes.  You can specify the row-index, provided by "item", along with the 0-based column index, to read the .Column property.  For example:

    With Me.MyListbox
    
        For Each item in .ItemsSelected
            
            Debug.Print .ItemData(item), .Column(0, Item), .Column(1, item)
    
        Next item
    
    End With


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by HTHP Wednesday, February 15, 2017 11:17 PM
    Wednesday, February 15, 2017 10:44 PM

All replies

  • You can use the Column property of the list box control for this. For example, to get the value of the 3rd column:

        Dim itm As Variant
        For Each itm In Me.List0.ItemsSelected
            Debug.Print Me.List0.Column(2, itm)
        Next itm

    where List0 is the name of the list box. The column number is zero-based, so use 0 for the first column, 1 for the second column etc.

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by HTHP Wednesday, February 15, 2017 11:17 PM
    Wednesday, February 15, 2017 10:40 PM
  • Using a ListBox set to allow selection of multiple items, is it possible to get the value of different columns?

    When I use For Each item in Me.ListBox.ItemsSelected, item is a variant, and I don't know how to extract any data from it except one value.

    Yes.  You can specify the row-index, provided by "item", along with the 0-based column index, to read the .Column property.  For example:

    With Me.MyListbox
    
        For Each item in .ItemsSelected
            
            Debug.Print .ItemData(item), .Column(0, Item), .Column(1, item)
    
        Next item
    
    End With


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by HTHP Wednesday, February 15, 2017 11:17 PM
    Wednesday, February 15, 2017 10:44 PM
  • Very helpful, thanks!
    Wednesday, February 15, 2017 11:17 PM