SQL statement to determine whether a known table exists

Source: Internet
Author: User
Tags sub command how to use sql

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

A: The solution is as follows:

Note: The following code is a module that typically references Dao.

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

End If

Next I

Set db = Nothing

End Function

Private Sub command 0_Click ()

FExistTable

End Sub

This event causes the 'parameter not option' error. After careful research, we found that fExistTable lacks a parameter, that is, the known table name is not reflected in the Code.

To:

Private Sub command 0_Click ()

FExistTable ("names of known tables to be determined ")

End Sub

End Sub no longer reports an error. After careful analysis, we use Dao to determine whether the database exists. If the value of fExistTable is True, it does not exist.

After the problem was solved, I suddenly remembered that the Access database also had a system table with an object name. Do you want to make a query to determine whether it was used?

After verification, the requirements can be met. If the value of Qty is greater than 0, the table already exists. Otherwise, the table does not exist.

SELECT Count (*) AS Qty

FROM MSysObjects

WHERE (MSysObjects. Name) Like "Name of a known table to be judged "));

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.