Results 1 to 8 of 8
  1. #1
    gstylianou is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Dec 2013
    Posts
    167

    delete Records except the first

    Hello everyone,




    Anyone know how its possible to delete all records from one table except the first; Is that possible?

    Regards

  2. #2
    Geo21 is offline Novice
    Windows 8 Access 2013
    Join Date
    May 2014
    Posts
    20

    delete Records except the first

    In the private sub type KeepFirst which will perform the below function. Have in mind Order By is very important as it will sort the records accordingly. I put ID as an example. You should put the field where the minimum value will not be deleted.
    Also change TableName to Actual Name of the Table.

    Code:
    Public Function KeepFirst() As Boolean
    Dim db as DAO.Database
    Dim rs As DAO.Recordset
    Dim strSQL As String
    
    strSQL = "SELECT TableName.* FROM TableName"
    strSQL =  strSQL + " ORDER BY TableName.ID"
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
    With rs
          If Not (.BOF And .EOF) Then
               .MoveLast
               While .RecordCount > 1
                    .Delete
                    .MoveLast
               Wend
          End If
          .Close
    End With
    Set db = Nothing
    
    End Function

  3. #3
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You might get a little better performance using the DESC keyword in the SQL

    strSQL = "SELECT TableName.* FROM TableName"
    strSQL = strSQL + " ORDER BY TableName.ID"

    to
    strSQL = "SELECT TableName.* FROM TableName"
    strSQL = strSQL + " ORDER BY TableName.ID DESC"

    Then the loop would like:
    Code:
          If Not (.BOF And .EOF) Then
               .MoveFirst
               While .RecordCount > 1
                    .Delete
                    .MoveNext
               Wend
          End If

  4. #4
    Geo21 is offline Novice
    Windows 8 Access 2013
    Join Date
    May 2014
    Posts
    20

    delete Records except the first

    Its me, with your way, is it MoveLast or MoveFirst?

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Sorry, movefirst. I will edit.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    A non-VBA approach:

    DELETE FROM tablename WHERE ID<>DMin("ID","tablename");

    That SQL can be a query object or can run it in VBA with the CurrentDb.Execute method.

    Or your first record could be determined by a date value.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    gstylianou is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Dec 2013
    Posts
    167
    Good morning from Cyprus,

    Thanks all about your help. I tried the vba code of Geo21 and works fine.!

    Thanks all again

  8. #8
    Geo21 is offline Novice
    Windows 8 Access 2013
    Join Date
    May 2014
    Posts
    20
    Kyprios, e?

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 11-08-2017, 09:04 AM
  2. Replies: 7
    Last Post: 11-08-2013, 03:34 AM
  3. Replies: 2
    Last Post: 01-24-2012, 02:16 PM
  4. Delete Records
    By dkeeper09 in forum Access
    Replies: 4
    Last Post: 10-28-2011, 01:41 PM
  5. delete records
    By fiamma68 in forum Queries
    Replies: 0
    Last Post: 11-15-2010, 06:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums