Wednesday, September 15, 2004

Deleting all data from an Access database

One of the questions I have had asked several times in class is how to empty a database of all existing data. You don't want to lose your tables structure, just the data. For example, you've created a new database and during the development process you have entered quite a bit of test data that needs to be removed before you release it to users. If done manually, this job can quickly become tedious. If your database has many tables, the following code will clear all the data in a hurry.


Dim ctr As Container, doc As Document, db As Database
Set db = CurrentDB()
Set ctr = db.Containiners!Tables
For Each doc in ctr.Documents
If Left$(doc.Name, 4) <> "MSys" Then 'Table is not a system table
db.Execute "DELETE [" & doc.Name & "].*" & _
"From [" & doc.Name & "];"
End If
Next doc

No comments: