Results 1 to 6 of 6

Thread: [RESOLVED] [Access 2003] Deleting all rows in a Table

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Resolved [RESOLVED] [Access 2003] Deleting all rows in a Table

    I have a Table in my Db which I use for holding information generated during a run.

    One of the requirements is that the Table be blank at the start of a run.

    I am currently using the following, however it has two flaws.

    One is that it takes a long time to run, the second is that after approx 7,000-
    9,000 deletions I get a file sharing lock count exceeded message.

    Can anyone advise as to a better/quicker way to delete all the rows in a table.

    Code:
    Sub DeleteAllRowsInTable(ByVal TableName as string)
    Dim RS As ADODB.Recordset
    Dim SqlStr As String
    Dim Counter As Long
    Dim StartTime As Date
        StartTime = Now
        
        SqlStr = "Select * from " & TableName
    
        Set RS = New ADODB.Recordset 'as we did with the connection
        RS.Open SqlStr, CurrentProject.Connection, adOpenKeyset, adLockPessimistic, adCmdText 'opening the recordset explained in the notes
           
        RS.MoveFirst 'moves to the first record
        Counter = 1
        Do Until RS.EOF = True 
            DoEvents
            Debug.Print Format(StartTime, "hh:nn:ss") & "  " & Format(Now, "hh:nn:ss") & "  " & Counter
            RS.Delete
            RS.Update
            RS.MoveNext 'moves next record
            Counter = Counter + 1
        Loop
        RS.Close
        Set RS = Nothing
    End Sub
    Signature Under Construction

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [Access 2003] Deleting all rows in a Table

    Why are you walking trough a recordset to perform a delete? Similar to issue a delete command
    Code:
    Dim strSQL as String
    strSQL = "Delete From " & tableName
    Now open your connection and execute on it
    
    Conn.Execute strSQL
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: [Access 2003] Deleting all rows in a Table

    The way you are doing it is very long winded,

    why not just run a truncate table command;

    vb Code:
    1. If ResetTable(tablename) = True then
    2.  'do stuff
    3. End If
    4.  
    5.  
    6. Public Function ResetTable(TableName as string) As Boolean
    7.  
    8. On Error GoTo errorhandler
    9.  
    10. Set adocom = New adodb.Command
    11.  
    12.  
    13. adocom.CommandText = "Truncate Table " & TableName
    14. adocom.CommandType = adCmdText   ' set the command type
    15.  
    16. adocom.ActiveConnection = CurrentProject.Connection' set the active connection
    17. adocom.Execute
    18.  
    19.  
    20. ResetTable = True
    21.  
    22. Set adocom = Nothing
    23.  
    24.  
    25. Exit Function
    26. errorhandler:
    27.     ResetTable = False
    28.  
    29. End Function
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [Access 2003] Deleting all rows in a Table

    If you are using Access VBA you can just do a
    Code:
    DoCmd.RunSQL("DELETE FROM yourtable")

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Location
    Best Place on Earth
    Posts
    363

    Re: [Access 2003] Deleting all rows in a Table

    Thanks guys,

    Tried all three methods and they work.
    Signature Under Construction

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    25

    Re: [Access 2003] Deleting all rows in a Table

    Quote Originally Posted by Torc View Post
    Thanks guys,

    Tried all three methods and they work.
    Hi all

    This information is also helpful to me.............thanks to all guys.....................

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width