Deleting all data from an Access database
Sometimes it may be necessary to delete all the data in a database
while retaining the table structure. 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.Containers!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
You might not want to delete data from linked tables. This can be easily
accommodated by checking the Connect property for each TableDef document.
The modifIEd code reads:
Dim ctr As Container, doc As Document, db As Database
Set db = CurrentDb()
Set ctr = db.Containers!tables
For Each doc In ctr.Documents
If Left$(doc.Name, 4) <> "MSys" And _
db.TableDefs(doc.Name).Connect = "" Then
注釋:Table is not a system table or a linked table
db.Execute "DELETE [" & doc.Name & "].*" & _
"From [" & doc.Name & "];"
End If
Next doc
You must also have cascading updates/deletes enabled for this procedure
to clear the data from all tables. As an alternative, run the code
multiple times. On the first pass through the database, the tables on
one side of the relationship are cleared, allowing the remaining tables
to be cleared on the next pass.
This tip was contributed by Dr. Michael S. Stoner, Henri Kover, and Stephen Bond