代码之家  ›  专栏  ›  技术社区  ›  Karthik

如何检查VB宏的MS Access中是否存在表[重复]

  •  13
  • Karthik  · 技术社区  · 16 年前

    可能重复:
    Check if access table exists

    我不熟悉VBA宏。知道如何检查表是否存在吗? 我搜索过以前的文章,但没有得到一个明确的解决方案。

    5 回复  |  直到 8 年前
        1
  •  30
  •   Karthik    10 年前

    通过设置对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
    
        2
  •  11
  •   Tobiasopdenbrouw    16 年前
    Exists = IsObject(CurrentDb.TableDefs(tablename))
    
        3
  •  9
  •   aaronsnoswell    11 年前

    我知道问题已经被回答了,但我发现现有的答案是无效的:
    对于具有非工作后端的链接表,它们将返回true。
    使用数据计数可以慢得多,但更可靠。

    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
    
        4
  •  1
  •   Community Mohan Dere    9 年前

    这不是一个新问题。我加进去了 comments in one SO post 并张贴 my alternative implementations 在另一个岗位。第一篇文章中的注释实际上阐明了不同实现之间的性能差异。

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

        5
  •  0
  •   Sjuul Janssen    16 年前

    Access有一些系统表 You can read about it a little here 可以启动以下查询以查看它是否存在(1=它存在,0=它不存在;)

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