How to use SQL statements to determine whether a known table exists

Source: Internet
Author: User
Tags table name access database how to use sql

How to determine if a known table exists in the library today, to help him solve this problem, the code for the usual reference DAO to do a module:

Function fExistTable(strTableName As String) As Integer
Dim db As Database
Dim i As Integer  
  Set db = DBEngine.Workspaces(0).Databases(0)  
  fExistTable = False
  db.TableDefs.Refresh
  For i = 0 To db.TableDefs.Count - 1
    If strTableName = db.TableDefs(i).Name Then
      'Table Exists
      fExistTable = True
      Exit For
    End If
  Next i
  Set db = Nothing
End Function
Private Sub 命令0_Click()
fExistTable
End Sub

A ' parameter not selectable ' error occurred with this event. After careful study, we found that fexisttable is missing the parameter, that is, the table name is not reflected in the code.

Modified to:

Private Sub 命令0_Click()
fExistTable(”需判断的已知表名”)
End Sub

End Sub no longer complains. Careful analysis, in fact, with "known table name" through DAO to determine whether there is a database, if the value of fexisttable is true is there, otherwise it does not exist.

Solve the problem, think of Access database also has system tables, stored with object name, can make a query to determine?

Verified by:

SELECT Count(*) AS Qty
FROM MSysObjects
WHERE (((MSysObjects.Name) Like "需判断的已知表名"));

can achieve requirements. If qty>0, it means that the table already exists, otherwise it does not exist.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.