如何从使用VBA循环多个表中的所有记录删除吗?访问2010 [英] How to delete all records from multiple tables with VBA loop? Access 2010

查看:258
本文介绍了如何从使用VBA循环多个表中的所有记录删除吗?访问2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个VBA循环功能,将删除所有的记录,从任何表的名称,如D2S _ *。

I want a VBA loop function that will delete all records from any table with a name like "d2s_*".

我发现code删除所有记录:

I've found code to delete all records:

DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM NameOfTable"
DoCmd.SetWarnings True

我还发现了一个循环,调用数据库中的每个表:

I've also found a loop to call each table in the database:

Dim T As TableDef
For Each T In CurrentDb.TableDefs
Debug.Print T.Name
Next T

所以,我想要做的就是将它们组合:

So what I want to do is combine them:

Dim T As TableDef
For Each T In CurrentDb.TableDefs
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM NameOfTable"
DoCmd.SetWarnings True
Next T

不过,据我所知,SQL查询中引用一个表名。有没有一种方法,使这种相对于任何表开始名为D2S _?

But as I understand, the SQL query has to reference a specific table name. Is there a way to make this relative to any table starting with the name "d2s_"?

推荐答案

尝试:

Dim T As TableDef
DoCmd.SetWarnings False
For Each T In CurrentDb.TableDefs
    If T.Name Like "d2s_*" Then
        DoCmd.RunSQL "DELETE * FROM " & T.Name
    End If
Next T
DoCmd.SetWarnings True

这篇关于如何从使用VBA循环多个表中的所有记录删除吗?访问2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆