If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > Delete item from Collection via Listbox?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
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.
Reply With Quote
  #2 (permalink)  
Old
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 11,651
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 11,651
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old
Registered User
 
Join Date: Nov 2012
Posts: 3
I got it. All i needed was ".Count" after lbCollection.ItemsSelected.

thanks for the steer
Reply With Quote
  #6 (permalink)  
Old
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 11,651
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On