Delete multiple selected records in DBgrid

Hi, my question is about DBGrid Data bound. I have a Database in Access 97 and when I try
to delete more than one register using del key, visual basic says to me that is impossible to
delete multiple records. However, in the Help there is a little code that must do that. This is:

Sub DeleteRows()
      Do While DBGrid1.SelBookmarks.Count <> 0
            Data1.Recordset.Bookmark = DBGrid1.SelBookmarks(0)
            Data1.Recordset.Delete
            Data1.Refresh
      Loop
End Sub

This code is not correctly (I mean).  Only removes on record.
Please give me a solution.
Thanks a lot.
netkukAsked:
Who is Participating?
 
swiltConnect With a Mentor Commented:
   Dim rst As Recordset
    Dim i As Integer
   
    Set rst = Data1.Recordset.Clone
    For i = 0 To DBGrid1.SelBookmarks.Count - 1
        rst.Bookmark = DBGrid1.SelBookmarks(i)
        rst.Delete
    Next i
    rst.Close
    Data1.Refresh

If in doubt, do a bit of cloning !
0
 
MikeP090797Commented:
For I = 0 to db1.selbookmars.count
data1.recordset.bookmark = db1.selbookmarks(i)
data1.recordset.delete
data1.refresh
next i

0
 
netkukAuthor Commented:
This solution requires an object at line 2.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
netkukAuthor Commented:
my answer is wrong. Sorry MikeP. The error that your program gives me is that the subindex is out of range.
0
 
zsiCommented:
Try changing the first line to:

     For i = 0 to db1.selbookmars.count -1
0
 
netkukAuthor Commented:
I'm sorry ZSI but your answer it doesn't run good. When I try to delete the records in DBgrid from this recordset of MSAcces, the selbookmarks return right the number of records that I have selected but when I delete the first, the next one is empty (bookmark=subindex out of range). It's possible to delete ?
Is my database wrong ?
0
 
zsiCommented:
netkuk,

Try deleting the records in reverse order.  This should alleviate the problem.

---------------------------------
For i = DBGrid1.SelBookmarks.Count - 1 to 0 Step -1
     Data1.RecordSet.Bookmark = DBGrid1.SelBookmarks(i)
     Data1.RecordSet.Delete
Next i
0
 
swiltCommented:
zsi,

unfortunately it don't work because as soon as you assigned to Data1.Recordset.bookmark the DBGrid1.SelBookmarks collection is cleared, probably because it is data bound to Data1.
0
 
zsiCommented:
Still, the point I wanted to make is that the loop should be executed in reverse order.  This will prevent the 'index out of range' error that netkuk experienced.
0
 
netkukAuthor Commented:
Congratulations ! You have reason. Thank you.
Second Zsi's solution is right, too. Congratulations, too.
Thank you a lot.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.