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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I enumerate through selected records?

Status
Not open for further replies.

rarich01

Programmer
Oct 22, 2001
14
0
0
US
Using Microsoft Access 97, I would like to create a custom delete button that will delete a set of records that was selected by the user. To do this, the user would open a form, in Datasheet view, use the record selector along the left side to choose the records they want deleted (by highlighting them), and finally, hit the custom delete button.

My problem is this: With the exception of the 'CurrentRecord', I don't know of any way to gain access to the OTHER records selected. Ideally, what I would like to have is a way to step through each record that was selected, and delete each one along the way. Is there anyway to do this? If the recordset object had an [ItemsSelected] property like the list box does, that would be great, but I can't seem to find anything like this.

If anybody has any suggestions on how I could do this, I would really appreciate it.

P.S. I tried the 'RunCommand acCmdDeleteRecord' option, however it still did not do what I wanted it to do. Any other suggestions would be appreciated.
 
Recommend you store the primary keys or unique values of the selected items in an array. Next, iterate through the array using the rs.Find or rs.Filter method to isolate your record, then call the rs.Delete command.

'Assume rs is a current Recordset object that contains
'data

Sub Delete()
Dim iBookMark as Integer

varray(0) = "ALFKI"
varray(1) = "BQRRS"
varray(2) = "JLKAA"


For i = 0 to UBound(varray) - 1
rs.Find "ID = " & varray(i)
rs.Delete, adBookmarkFirst 'The first record in the RS
Next i
rs.Requery 'If you want to refresh your data
End Sub
 
I see how you go about iterating through the array to delete each item in the array. I have no problem with that. My problem, however, is how to get those PrimaryKeys in the first place? Like I mentioned before, if I select several records from a Datasheet view, I have access to only one of the records' info, namely, the [CurrentRecord]. No Problem, but the question is, how do I get access to the information of the OTHER records that were selected.

Any ideas?
 
A suggestion:

Use your delete button to open a second form. On that form have a listbox control set to multi-select and with the recordsource set to the table that you want to delete from. Select the records to be deleted in the listbox and use the listsource.selected property to delete each record in turn.


HTH

Lightning
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top