vba DoCmd.DeleteObject acTable 与 acTable 有什么区别?掉落表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15945297/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:22:07  来源:igfitidea点击:

What is the difference between DoCmd.DeleteObject acTable Vs. DROP TABLE

vbams-accessaccess-vba

提问by Justin

Details:

细节:

I have an MS-Access Database procedure where I create tables locally in the database. However, I want to ensure that the tables I create are tested for and if the test fails I need to delete/drop the other tables that have been created. Basically a rollback procedure I guess.

我有一个 MS-Access 数据库过程,我在数据库中本地创建表。但是,我想确保我创建的表经过测试,如果测试失败,我需要删除/删除已创建的其他表。我猜基本上是一个回滚程序。

Question:

题:

I came across the two methods to delete tables but cannot figure out if one has more pro than cons etc...

我遇到了两种删除表格的方法,但无法弄清楚一种方法是否比缺点更多,等等......

Can someone tell me what the difference is?

有人能告诉我有什么区别吗?

Many Thanks!

非常感谢!

回答by Gord Thompson

DoCmd.DeleteObject acTable, "aaaTest"

...and...

...和...

Dim cdb As DAO.Database
Set cdb = CurrentDb
cdb.Execute "DROP TABLE [aaaTest]", dbFailOnError

...and...

...和...

Dim cdb As DAO.Database
Set cdb = CurrentDb
cdb.TableDefs.Delete "aaaTest"

...are all just different ways of accomplishing the same thing. They delete the local TableDefobject with that name (either an actual local table, or a table link).

...都是完成同一件事的不同方式。他们删除TableDef具有该名称的本地对象(实际的本地表或表链接)。

回答by Ben

@gordthompson did a concise job of explaining three ways to delete tables. In testing his methods I noticed one difference. I'm working offline and have linked tables in a back-end that point to Access tables on the client network. When I try to delete the linked tables using the Access UI it can take over 30 seconds for each table. It's annoying.

@gordthompson 简洁地解释了三种删除表的方法。在测试他的方法时,我注意到一个不同之处。我正在离线工作,并在后端链接了指向客户端网络上的 Access 表的表。当我尝试使用 Access UI 删除链接表时,每个表可能需要超过 30 秒。这很烦人。

Based on Gord's examples I have discovered that...

根据戈德的例子,我发现......

DoCmd.DeleteObject acTable, "aaaTest" ' is very slow, just like the Access UI.

CurrentDb.Execute "DROP TABLE [aaaTest]", dbFailOnError ' is immediate.

CurrentDb.TableDefs.Delete "[aaaTest]" ' is also immediate

If you have a table name that contains a dash or other special character, wrapping the name in [square brackets] should solve the problem.

如果您的表名包含破折号或其他特殊字符,将名称括在 [方括号] 中应该可以解决问题。

回答by Usefull

I just found out that DropTable doesn't like table names with a dash in them. I had quite a few, so I used DoCmd.Rename to rename the table right before I used droptable to erase them. This was the easiest fix for me because I had 15 table with dashes in them and 60 all together that I delete upon exit to clean up temporary files.

我刚刚发现 DropTable 不喜欢带有破折​​号的表名。我有很多,所以我在使用 droptable 擦除它们之前使用 DoCmd.Rename 重命名表。这对我来说是最简单的解决方法,因为我有 15 个带有破折号的表,还有 60 个在退出时删除以清理临时文件。