如何使用 VBA 循环从多个表中删除所有记录?访问 2010
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20530274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete all records from multiple tables with VBA loop? Access 2010
提问by Trevor D
I want a VBA loop function that will delete all records from any table with a name like "d2s_*".
我想要一个 VBA 循环函数,它将删除任何名称为“d2s_*”的表中的所有记录。
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
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_"?
但据我了解,SQL 查询必须引用特定的表名。有没有办法使它相对于以名称“d2s_”开头的任何表?
回答by Monty Wild
Try:
尝试:
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