Results 1 to 11 of 11
  1. #1
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Brisbane, Queensland, Australia
    Posts
    387
    Thanks
    0
    Thanked 1 Time in 1 Post

    Selecting rows in a list box in code (XP)

    I know how to iterate through the selected items in a list box but I can't figure out how to iterate through all the items and select rows using the value in column(0)
    its a multi select list box

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Selecting rows in a list box in code (XP)

    Can you explain in more detail what you want to accomplish?

  3. #3
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Brisbane, Queensland, Australia
    Posts
    387
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Selecting rows in a list box in code (XP)

    I have two tables (items and Types) with a joining table (Itemtypes). the list box source is the Types table. I am trying to show as selected the types that are in the itemtype table for the current item.
    The user can then add itemtype records for the current item by clicking on the type record in the list box. the click event then adds a record for the item and the type selected to the itemtype table. unselecting the item deletes the record.
    so in the current event I want to select the items for which there is a record. something like -
    for each introw in listbox.items
    if listbox.column(0) = lngID then
    listbox.selected(introw) = true
    end if
    next

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Selecting rows in a list box in code (XP)

    Although it's possible to use a multi-select list box for this, I'd use a subform based on the ItemTypes table instead. It's much easier to manage - you don't need code to add or remove records, it's done automatically

  5. #5
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Brisbane, Queensland, Australia
    Posts
    387
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Selecting rows in a list box in code (XP)

    Not really an option in this case - the list box method allows a one click functionality for the user and good graphical representation in a small amount of space. there are three list boxes on a small tab.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Selecting rows in a list box in code (XP)

    But think of the overhead - each time the user accidentally clicks an item, a record has to be created or deleted.
    A subform doesn't need to take up a lot of space, it can be made to look more or less like a list - see the attached example.
    Attached Files Attached Files

  7. #7
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Brisbane, Queensland, Australia
    Posts
    387
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Selecting rows in a list box in code (XP)

    Yep, used thousands of them but not for this. the user want to see all of the options, without having to drop a combo and see if they are on the list or not.. It's more about making it easy for the user not for me.

    if i use -
    For intRow = 0 To ctlListBox.ListCount - 1
    Debug.Print ctlListBox(intRow).Column(0) ' this row is trying to get the value of the row but it doesn't work.
    Next intRow

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Selecting rows in a list box in code (XP)

    See the attached sample, it uses the same tables as the previous one, but it uses a list box to display the selected classed for a student.

    Please note that the previous example didn't contain any VBA code, while this one needs some 80 lines of code.
    Attached Files Attached Files

  9. #9
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Brisbane, Queensland, Australia
    Posts
    387
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Selecting rows in a list box in code (XP)

    Got it -
    For intRow = 0 To ctlListBox.ListCount - 1
    Debug.Print ctlListBox.ItemData(intRow) ' returns the value of the bound column
    Next intRow

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Selecting rows in a list box in code (XP)

    I just noticed that the sample database that I attached to my previous reply doesn't handle new records correctly.
    Here is a slightly improved version - even more code
    Attached Files Attached Files

  11. #11
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Brisbane, Queensland, Australia
    Posts
    387
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Selecting rows in a list box in code (XP)

    I had one other question but found the answer in your sample, so once again, many thanks for your help Hans.

Posting Permissions

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