[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Delete multiple selected records in DBgrid

Posted on 1998-05-09
10
Medium Priority
?
214 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
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 480 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

834 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