tags:

views:

165

answers:

3

How to delete all rows from table before starting inserting them?

+1  A: 

If you are programming vba I assume that you are working with Adodb or simply ADO. Therefore in order to delete a table records you can use a command object to do that.

Dim cnn as Connection
Dim cmd as Command

Set cnn=new Connection()
cnn.ConnectionString="ConnectionString"
cnn.Open()

Set cmd=new Command()
cmd.ActiveConnection=cnn
cmd.CommandText="DELETE FROM MyTable"
cmd.Execute()

cnn.Close()

Updated: In order to use ADO objects you should add a reference to ADODB library

Beatles1692
I use DAO, not ADO
Tom
I recommend you use ADO instead of DAO
Beatles1692
It shouldn't really matter which you use.
Oorang
"If you are programming vba I assume that you are working with Adodb or simply ADO." I'm glad you provided this "I assume" qualifier in your opening. But the assumption is unwarranted and unhelpful. Though you may believe ADO a great technology, the user's need, as presented, doesn't suggest any need to introduce ADO.
Smandoli
Meh... Both are referenced automatically when a database is created. The DAO vs. ADO debate was only relevant when people thought MS might eventually get rid of DAO or at least replace the DAO parts of the Access object model with ADO. But they never did and now ADO itself has given way to ADO.Net. And DAO is still there, and even RDO is still there. I don't think it matters one little bit which you use any more.
Oorang
+2  A: 

Two things. First, DAO vs ADO is almost irrelevant. They are both available to Access up through Windows 7 and AFAIK there are no plans to remove them. Either should be OK to use in Access. Secondly, you can just do this:

Public Sub Example()
    With Access.CurrentDb
        .Execute "DELETE Table2.* FROM Table2;"
        .Execute "INSERT INTO Table2 ( fld1, fld2 ) SELECT Table1.ID, Table1.MyField FROM Table1;"
    End With
End Sub

You could just to this:

Public Sub Example()
    With Access.DoCmd
        .RunSQL "DELETE Table2.* FROM Table2;"
        .RunSQL "INSERT INTO Table2 ( fld1, fld2 ) SELECT Table1.ID, Table1.MyField FROM Table1;"
    End With
End Sub

But the Execute method throws more informative error messages (and if you still care, is DAO).

Oorang
I'd like to add a point on the DAO and ADO topic. Beatles1692 offers an ADO method. Oorang offers a method that is NEITHER one or the other -- it's an Access VBA method.
Smandoli
Actually, that method is DAO;) If you check, currentdb is property if the DAO.Database type. RunSQL is method of the DAO.Database object. It's just that Access has integrated DAO into it's own object model more completely then ADO.
Oorang
A: 

DAO Recordsets don't have an effective way to empty the table, as far as I know. If it's a small table, you can probably delete record-by-record, but I would find that silly. I just use DoCmd.RunSQL to run a DELETE query. I would typically set SetWarnings to False temporarily, of course.

Since this is separate from DAO, I would see that operation through before opening the recordset properly.

Smandoli