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.查看 http://www.qqread.com/access/t275768.html
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- ACCESS入门教程(一)初识Access 200 (53439次浏览)
- ACCESS入门教程(二)窗口接口使用简介 (29045次浏览)
- ACCESS入门教程(三)用向导建立数据库 (25665次浏览)
- ACCESS入门教程(九)建立简单的查询 (21179次浏览)
- ACCESS入门教程(十一)如何建立数据窗体 (14937次浏览)
- ACCESS入门教程(六)在表中输入数据 (14445次浏览)
- ACCESS入门教程(十)为查询制定规则 (13204次浏览)
- ACCESS入门教程(五)用表向导建立表 (11480次浏览)
- 中文Access2000速成教程--1.2使用“数据库向 (597次浏览)
- 用Access设计客观试卷(一) (382次浏览)



