How do you delete the contents of a table using VBA?

mikewood1980

Registered User.
Local time
Today, 05:51
Joined
May 14, 2008
Messages
45
Hi

I would like to delete the contents of one of my tables in the database using code. Does anyone know how to do this. Table is already declared with the following code...

Code:
    Dim dbCurr As Database
    Set dbCurr = DBEngine.Workspaces(0).Databases(0)
    Dim rsTableTest As Recordset
    Set rsTableTest = dbCurr.OpenRecordset("tblTableTest")

    'need to delete the table here ***********

can anyone help? :)

Thanks
MikeWood
 
Hi

I would like to delete the contents of one of my tables in the database using code. Does anyone know how to do this. Table is already declared with the following code...

Code:
    Dim dbCurr As Database
    Set dbCurr = DBEngine.Workspaces(0).Databases(0)
    Dim rsTableTest As Recordset
    Set rsTableTest = dbCurr.OpenRecordset("tblTableTest")
 
    'need to delete the table here ***********

can anyone help? :)

Thanks
MikeWood

You would be much better off to use a SQL statement in code to delete your records. If you are deleting everything I'd use something like.

Code:
dbCurr.Execute "DELETE * FROM tblTableTest"

Cycling through a recordset just to delete records would be wasted effort, now if you need to do that for some unexplained reason then you could use something like

Code:
Do Until rsTableTest.EOF
     rsTableTest.Delete
     rsTableTest.MoveNext
Loop
 
Simply use one line code

USING ADO
currentproject.connection.execute"delete * from tblTableTest"

Or

USING DAO
currentdb.execute"delete * from tblTableTest"
 
If it is everything, I would create a delete query, then use DOCMD to run it. much easier to maintain.
 

Users who are viewing this thread

Back
Top Bottom