删除access数据库中的所有数据-钱玉炜
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> 综合其它


删除access数据库中的所有数据

发表时间:2008/7/19 8:22:06 评论(0) 浏览(6432)  评论 | 加入收藏 | 复制
   
摘 要:删除Access数据库中的所有数据
正 文:

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


Access软件网交流QQ群(群号:198465573)
 
 相关文章
清空表全部记录后重置自动编号起始值--另一方法  【赵文斌  2012/5/17】
【Access入门】清空指定的表中记录,删除表中所有数据的示例,清...  【麥田  2012/10/8】
【Access入门】清空表中指定字段的记录,删除表中指定列的所有数...  【麥田  2012/10/10】
2012世界末日了吗?被玛雅人耍了吧--用For Each循环方式...  【麥田  2012/12/21】
【Access教程】Access清空表后让编号复位  【漏蛧尐魚℡  2013/6/27】
常见问答
技术分类
相关资源
文章搜索
关于作者

钱玉炜

文章分类

文章存档

友情链接