Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2004
    Posts
    145

    Unanswered: Select All items in a listbox

    Hello,
    I've implemented some code to select each item in a list box using the selected property:

    lst.selected(i) = True

    However after doing so there are no items in the listbox's ItemsSelected property.

    It is nice iterating through the ItemsSelected property, but it doesn't seem to work unless rows are manually selected in the listbox.

    I've already found a work around that doesn't use the ItemsSelected property so I don't need alternative methods. So my question is how to set the selected property to true for each listbox row programatically and make it work with the ItemsSelected property.
    Thanks
    Gunner
    Last edited by gwgeller; 09-04-07 at 16:10.
    GG
    Code:
    On Error Goto Hell
    
    Hell:
        Msgbox("An error occurred, but was sent to Hell. You may continue.")

  2. #2
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    Not sure what you're running into. This worked in a quick test (A2k):

    Code:
      Dim ctl           As Control
      Dim varItem       As Variant
      Dim x             As Integer
    
      Set ctl = Me.List0
    
      For x = 0 To Me.List0.ListCount - 1
        Me.List0.Selected(x) = True
      Next x
    
      For Each varItem In ctl.ItemsSelected
        Debug.Print ctl.ItemData(varItem)
      Next varItem
    Paul

  3. #3
    Join Date
    May 2004
    Location
    New York State
    Posts
    1,178
    Gunner,

    I don't understand something. If you don't mind my asking, if you want all the values in the listbox, why don't you simply use the listbox source in a table or query?

    Sam

  4. #4
    Join Date
    Jan 2004
    Posts
    145
    Got a little side tracked.

    pbaldy, I tried your code and it works. I'm not sure what I was doing because I thought I used the same method. There are multiple listboxes, maybe I was testing the wrong listbox. Anyway it works which is all that matters, thank you.

    Sam, it would be simple if I only needed all the items in the listbox, but the user selects if they want one, multiple or all the items. Using the selections I create an "IN" clause for a dynamic query. IE. "custID IN(1,2,3,4)". I could test if the select all check box is select and just leave the "IN" off, but it would be less code to just put everything in an "IN" clause since it would cover all three situations. The lists aren't too big so I wouldn't have an "IN" clause with hundreds of entries.
    To add to the "mess" there are several listboxes that the user uses to narrow down the data. So there are several "IN" clauses. So the query created using the values from the listboxes is different than the listbox sources. I hope this clears things up.

    Thanks for the responses.
    GG
    Code:
    On Error Goto Hell
    
    Hell:
        Msgbox("An error occurred, but was sent to Hell. You may continue.")

Posting Permissions

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