Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2012
    Posts
    3

    Delete item from Collection via Listbox?

    All,

    I am a beginner with VBA and have hit a brick wall with the following:

    I load every item within my persons collection (colPersons) to a listbox (lbCollection). This works with no problems.

    I want to be able to click a button (cmdDelete) which will remove the item i have selected on my listbox from my collection.

    Any help would be very much appreciated.

    Kevin.

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    12,524
    so you need to read up on list bioxes, and how to know which items have been selected
    (look at the itemselected property)
    then retrieve the key and or the text

    then iterate through your persons collection till you find that key and or name, then delete that ietm from the persons collection
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Nov 2012
    Posts
    3
    Thank you for the response healdem.

    This is (i believe) what i've been doing but to no avail. The code i have for my 'Delete' button is:

    Private Sub cmdDelete_Click()
    colPersons.Remove (lbCollection.ItemsSelected)
    End Sub


    colPersons = My Collection
    lbCollection = My listbox which displays my collection data

    When clicking 'Delete' i get a Compile Error: Argument not optional

    I would hugely appreciate further help getting over this frustrating hurdle!!!

    thanks

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    12,524
    so what is the value of lbCollection.ItemsSelected ?
    is that value appropriate for the remove method
    if so is that value actually referring to the item you want to delete in colPersons?

    https://www.google.co.uk/#hl=en&tbo=...w=1631&bih=826

    Error Handling and Debugging Tips for Access 2007, VB, and VBA

    MS Access Tutorial: Debugging VBA Code in Access 2003/XP/2000/97
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    Nov 2012
    Posts
    3
    I got it. All i needed was ".Count" after lbCollection.ItemsSelected.

    thanks for the steer

  6. #6
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    12,524
    I'd do some very thorough testing of that bit of code if I were you.
    Unless you are keeping the list box and the collection in sync then it could easily get out of step. make certain the correct item is being deleted in the collection

    not knowing your application and the way you have designed it, its hard to be deifinitve but there is a risk you may not be doing what you think you are.
    I'd rather be riding on the Tiger 800 or the Norton

Posting Permissions

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