vba 如何检查 MS Access 中是否存在用于 vb 宏的表

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

How to check if a table exists in MS Access for vb macros

ms-accessvbaaccess-vba

提问by Karthik

Possible Duplicate:
Check if access table exists

可能重复:
检查访问表是否存在

I'm new to vba macros. Any idea how to check if a table exists or not? I have searched for previous posts but did not get a clear solution for this.

我是 vba 宏的新手。知道如何检查表是否存在吗?我已经搜索了以前的帖子,但没有得到明确的解决方案。

回答by Karthik

Setting a reference to the Microsoft Access 12.0 Object Library allows us to test if a table exists using DCount.

设置对 Microsoft Access 12.0 对象库的引用允许我们使用 DCount 测试表是否存在。

Public Function ifTableExists(tblName As String) As Boolean

    If DCount("[Name]", "MSysObjects", "[Name] = '" & tblName & "'") = 1 Then

        ifTableExists = True

    End If

End Function

回答by Tobiasopdenbrouw

Exists = IsObject(CurrentDb.TableDefs(tablename))

回答by Patrick Honorez

I know the question is already answered, but I find that the existing answers are not valid:
they will return True for linked tables with a non working back-end.
Using DCount can be much slower, but is more reliable.

我知道问题已经得到解答,但我发现现有的答案无效:
对于具有非工作后端的链接表,它们将返回 True。
使用 DCount 可能会慢得多,但更可靠。

Function IsTable(sTblName As String) As Boolean
    'does table exists and work ?
    'note: finding the name in the TableDefs collection is not enough,
    '      since the backend might be invalid or missing

    On Error GoTo hell
    Dim x
    x = DCount("*", sTblName)
    IsTable = True
    Exit Function
hell:
    Debug.Print Now, sTblName, Err.Number, Err.Description
    IsTable = False

End Function

回答by David-W-Fenton

This is not a new question. I addresed it in comments in one SO post, and posted my alternative implementationsin another post. The comments in the first post actually elucidate the performance differences between the different implementations.

这不是一个新问题。我在一篇 SO post 的评论中提到了它,并在另一篇文章中发布了我的替代实现。第一篇文章中的评论实际上阐明了不同实现之间的性能差异。

Basically, which works fastest depends on what database object you use with it.

基本上,哪种工作最快取决于您使用的数据库对象。

回答by Sjuul Janssen

Access has some sort of system tables You can read about it a little hereyou can fire the folowing query to see if it exists ( 1 = it exists, 0 = it doesnt ;))

Access 有某种系统表您可以在这里阅读一下,您可以触发以下查询以查看它是否存在(1 = 存在,0 = 不存在;))

SELECT Count([MSysObjects].[Name]) AS [Count]
FROM MSysObjects
WHERE (((MSysObjects.Name)="TblObject") AND ((MSysObjects.Type)=1));