?
Solved

Delete multiple selected records in DBgrid

Posted on 1998-05-09
10
Medium Priority
?
210 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

743 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