Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listboxes and deleting records

Status
Not open for further replies.

brew2

Programmer
Joined
Apr 29, 2007
Messages
21
Location
US
I have two lstboxes, one access db with two tables with a one to many relationship. lstbox1 is filled from table1(one to many relationship) and lstbx2 is filled from table2 (many in the one to many relationship).

Before the item in lstbox1 is deleted all relationships listed in lstbox2 must be deleted first. That can be an arduous task if lstbox2 has bunches of records. Therefore, it would seem that if the items in lstbox2 can be counted, then why not be able to add each item to a collection and then delete them one at a time.

For this discussion lstbox1 is lstTeam and lstbox2 is lstPlayer. Each has a separate Class, CTeam and CPlayer.

Something like this:
Code:
dim i as integer 
dim c as new collection 
dim obj as object 
dim objPlayer as object 
dim CT as CTeam 
dim CP as CPlayer 
obj = lstTeam.SelectedItem 
objPlayer = lstPlayer.SelectedItem 'only works if item is highlighted 
CT = CType(obj, CTeam) 
CP = CType(objPlayer, CPlayer) 

For i = 0 To lstBox2.Items.Count 
     c.Add(CP, CP.PID) 
Next i

The For Next loop will add item highlighted in the lstbox but the second time thru tries to add it again which it can not do. Is there a way to cycle thru the lstbox and add each item to the collection which can then be put into another For Next loop to delete the players before the team is deleted from the tables?

Thank you.
 
Why not use an SQL DELETE statement with the WHERE clause containing the appropriate ID value?


Hope his helps.

[vampire][bat]
 
Guess I did not make it clear that if lstbox2 has bunches (more than a hundred) of items, deleting one at a time is a time consuming task. Since the idea is to delete the item in lstbox1 and it has relations in lstbox two I am looking for some way to loop thru lstbox2, place them in a collection and then delete them with another loop.

Maybe there is a better way than a collection?

Thanks.
 
I don't see the problem with my earlier suggestion. Have you tried it?

[vampire][bat]
 
The question is how to pick up the next item in the list to get the appropriate ID. Deleting with the appropriate ID is easy to do one at a time.

Do you have any suggestions on how to loop and get the next and then add it to a collection?
 
No collection needed. Cycled through the items in the lstbox deleted the highlited one, reset the seleceditem. Used a for each next and the items.count method.

Thanks for the help.
 
<Why not use an SQL DELETE statement with the WHERE clause containing the appropriate ID value?

<Guess I did not make it clear that if lstbox2 has bunches (more than a hundred) of items, deleting one at a time is a time consuming task.

I don't see where the first quote implies the conclusion in the second quote.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top