Solved

Delete multiple selected records in DBgrid

Posted on 1998-05-09
10
208 Views
Last Modified: 2006-11-17
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.
0
Comment
Question by:netkuk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1444894
For I = 0 to db1.selbookmars.count
data1.recordset.bookmark = db1.selbookmarks(i)
data1.recordset.delete
data1.refresh
next i

0
 

Author Comment

by:netkuk
ID: 1444895
This solution requires an object at line 2.
0
 

Author Comment

by:netkuk
ID: 1444896
my answer is wrong. Sorry MikeP. The error that your program gives me is that the subindex is out of range.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:zsi
ID: 1444897
Try changing the first line to:

     For i = 0 to db1.selbookmars.count -1
0
 

Author Comment

by:netkuk
ID: 1444898
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
 
LVL 2

Accepted Solution

by:
swilt earned 120 total points
ID: 1444899
   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
 
LVL 4

Expert Comment

by:zsi
ID: 1444900
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
 
LVL 2

Expert Comment

by:swilt
ID: 1444901
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
 
LVL 4

Expert Comment

by:zsi
ID: 1444902
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
 

Author Comment

by:netkuk
ID: 1444903
Congratulations ! You have reason. Thank you.
Second Zsi's solution is right, too. Congratulations, too.
Thank you a lot.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question