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