Solved

Delete multiple selected records in DBgrid

Posted on 1998-05-09
10
203 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
  • 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now