Microsoft Knowledge Base Email Alertz

When deleting records in a loop using the Microsoft Jet 4.0 OLE DB provider, not all records get deleted from the table.

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 253561 - Last Review: September 26, 2005 - Revision: 1.1

FIX: Delete/MoveNext Moves to Wrong Record with Jet 4.0 Provider

This article was previously published under Q253561

On This Page

SYMPTOMS

When deleting records in a loop using the Microsoft Jet 4.0 OLE DB provider, not all records get deleted from the table.

CAUSE

When using forward-only, server-side cursors, the Microsoft Jet 4.0 OLE DB provider incorrectly positions the cursor after executing the Delete method.

RESOLUTION

To resolve this problem, do one of the following:
  • Use a different cursor type, such as Keyset.
  • Use a SQL statement, such as: DELETE * FROM MyTable.

STATUS

This has been fixed in Microsoft Jet 4.0 SP4.

MORE INFORMATION

The problem manifests itself only when using forward-only, server-side cursors and deleting records in a loop similar to this:
Do While Not rs.EOF
   rs.Delete
   rs.MoveNext
Loop
				
With a 10-record table:
  • With the Microsoft Jet 3.51 OLE DB provider (MSJTOR35.DLL 3.52.1527.4), the loop gets executed 10 times.
  • With the Microsoft Jet 4.0 OLE DB provider from MDAC 2.1 GA, the loop gets executed five times.
  • With the Microsoft Jet 4.0 OLE DB provider from MDAC 2.1 SP2 (JETOLEDB40.DLL 4.00.2927.2), the loop gets executed twice.
  • With the Microsoft Jet 4.0 OLE DB provider (JETOLEDB40.DLL 4.00.3714.0), the loop gets executed 10 times, as expected.

Steps to Reproduce Behavior

  1. In Microsoft Visual Basic 5.0 or 6.0, create a new Standard EXE project. Form1 is created by default.
  2. On the Project menu, select References, and then add the following type library:
    Microsoft ActiveX Data Objects 2.1 Library
  3. Add a Command button (Command1) and the following code to the default form:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim cn As ADODB.Connection, rs As ADODB.Recordset, I As Long
      Set cn = New ADODB.Connection
      cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=nwind.mdb"
      cn.Execute "CREATE TABLE DB12374 (ID Int)"
      For I = 1 To 10
        cn.Execute "INSERT INTO DB12374 VALUES (" & I & ")"
      Next I
      Set rs = New ADODB.Recordset
      rs.Open "SELECT * FROM DB12374", cn, adOpenForwardOnly, adLockOptimistic, adCmdText
      I = 0
      Do While Not rs.EOF
        I = I + 1
        Debug.Print I, "Deleting"; rs(0)
        rs.Delete
        rs.MoveNext
      Loop
      rs.Close
      cn.Execute "DROP TABLE DB12374"
      cn.Close
    End Sub
    					
    NOTE: You might need to adjust the connect string to point to a valid Jet database.

  4. Execute the project. If you have an older version of the provider, you should get output similar to the following:
     1            Deleting 1 
     2            Deleting 2 
    					
    If you have the latest version of the provider, or change the cursor type from adOpenForwardOnly to adOpenKeyset, the output should be:
     1            Deleting 1 
     2            Deleting 2 
     3            Deleting 3 
     4            Deleting 4 
     5            Deleting 5 
     6            Deleting 6 
     7            Deleting 7 
     8            Deleting 8 
     9            Deleting 9 
     10           Deleting 10 
    					

APPLIES TO
  • Microsoft OLE DB Provider for Jet 4.0
Keywords: 
kbhotfixserver kbqfe kbbug kbfix kbjet KB253561
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate