VB 6.0 determines whether the specified table exists in Access 2010

Source: Internet
Author: User
Tags goto

Problem

Recently encountered an issue in working with VB6 to determine whether a specified table exists in the Access 2010 database. For earlier Access databases (which should be primarily Access 2003 and previous versions), there is no problem with DAO engine operations. The main relevant code is as follows:

For each mytable in MyDatabase. TableDefs
Print Mytable.name
Next

However, DAO technology is not available for access 2010 anymore. To this end, I use Baidu Search, a preliminary approach to get the following:

Cn2. Open "PROVIDER=MSDASQL.1; Persist Security info=false;data source=ms Access database;initial catalog= "+ App.Path +" \data\object.accdb "

Debug.Print Cn2. ConnectionString

' 1, first determine if the table exists
Cn2. Execute "SELECT * from single"
If Err.Number <> 0 Then

MsgBox "table in target database single does not exist! "
GoTo End1
End If

Note that I use VB6 to access ACCESS2010 when I'm not using microsoft.jet.oledb.4.0 technology, and I find it impossible to use it to access a higher version of Access (including 2007), which you believe is no problem. We see that the above code uses a trap technique to analyze whether a specified table exists in the database. But, alas, the above techniques are useless for access 2010 (I have tried it myself).

Answer

The correct method (one) is to use the OpenSchema method of the ADO object. The relevant code is as follows:

Cn2. Open "PROVIDER=MSDASQL.1; Persist Security info=false;data source=ms Access database;initial catalog= "+ App.Path +" \data\object.accdb "
Dim byes as Boolean
byes = False

Set rs2 = cn2. OpenSchema (adSchemaTables)

Do Until rs2. Eof

Debug.Print rs2! table_name

If rs2! table_name = "single" Then
byes = True
GoTo Cont1
End If

Rs2. MoveNext

Loop

Cont1:
Rs2. Close

If not byes Then
GoTo End1
End If


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.