用于检查 MS ACCESS 上是否存在表的 VBA 脚本,如果存在则删除 [英] VBA Script to check if table exist on MS ACCESS, Delete if it does

查看:43
本文介绍了用于检查 MS ACCESS 上是否存在表的 VBA 脚本,如果存在则删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行检查以查看 TableA 是否存在,如果存在,则删除整个表.如果没有,我将创建表.我真的不知道这是否可以在 VBA/MS Access 上实现.

I need to implement a check to see if TableA exists, if it does, drop the entire table. If it doesn't, i will create the table. I couldn't really find out if this is possible to implement on VBA / MS Access.

在 SQL 中我们可以使用:

In SQL we can use:

DROP TABLE IF EXISTS dbo.TableA

有人知道如何实现吗?谢谢!

Anybody has any idea how this can be implemented? Thank you!

推荐答案

考虑使用 TableDefs 集合,您可以在其中迭代项目并使用在代码中传递的相同 DDL SQL 语句有条件地删除或创建.

Consider using the TableDefs collection where you iterate through items and conditionally drop or create using same DDL SQL statements passed in code.

Dim db As Database 
Dim tbldef As TableDef

Set db = CurrentDb

For each tbldef in db.TableDefs 
   If tbldef.Name = "TableName" Then 
      db.Execute "DROP TABLE " & tbldef.Name, dbFailOnError
   End if 
Next tbldef

db.Execute "CREATE TABLE TableName (...rest of SQL...);", dbFailOnError    

' UNINITIALIZE OBJECTS 
Set tbldef = Nothing 
Set db = Nothing

这篇关于用于检查 MS ACCESS 上是否存在表的 VBA 脚本,如果存在则删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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