Article ID: 186246
Article Last Modified on 12/4/2007
Set recordset = connection.OpenSchema (QueryType, Criteria, SchemaID)
QueryType Criteria
=============================
adSchemaTables TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
TABLE_TYPE
Use adSchemaTables to list the tables in a database.
Set rs = cn.OpenSchema(adSchemaTables)
While Not rs.EOF
Debug.Print rs!TABLE_NAME
rs.MoveNext
Wend
To list only the tables in the Access Nwind database, use:
Set rs = cn.OpenSchema(adSchemaTables, _
Array(Empty, Empty, Empty, "Table")
Use the same syntax, using the OLE DB Provider for ODBC with the Jet
ODBC driver and using the Jet OLE DB Providers. Set rs = cn.OpenSchema(adSchemaTables)To list just the tables in the Microsoft SQL Server Pubs database, use:
Set rs = cn.OpenSchema(adSchemaTables, _
Array("Pubs", Empty, Empty, "Table")
Use the same syntax using the OLE DB Provider for ODBC with the SQL
Server ODBC driver and using the OLE DB Provider for SQL Server.
QueryType Criteria
===============================
adSchemaColumns TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
Use adSchemaColumns to list the fields in a table. Set rs = cn.OpenSchema(adSchemaColumns,Array(Empty, Empty, "Employees") While Not rs.EOF Debug.Print rs!COLUMN_NAME rs.MoveNext WendThis works using the OLE DB Provider for ODBC with the Jet ODBC Driver and using with the Jet OLE DB Providers.
Set rs = cn.OpenSchema(adSchemaColumns, Array("pubs", "dbo", "Authors")
Note that TABLE_CATALOG is the database and TABLE_SCHEMA is the table
owner. This works using the OLE DB Provider for ODBC with the SQL Server ODBC
driver and using the OLE DB Provider for SQL Server.
QueryType Criteria
================================
adSchemaIndexes TABLE_CATALOG
TABLE_SCHEMA
INDEX_NAME
TYPE
TABLE_NAME
You provide the index name in case of adSchemaIndexes querytype.
Set rs = cn.OpenSchema(adSchemaIndexes, _
Array(Empty, Empty, Empty, Empty, "Employees")
While Not rs.EOF
Debug.Print rs!INDEX_NAME
rs.MoveNext
Wend
This works using the OLE DB Provider for ODBC with the Jet ODBC Driver
and using with the Jet OLE DB Providers.
Set rs = cn.OpenSchema(adSchemaIndexes, _
Array("Pubs", "dbo", Empty, Empty, "Authors")
This works using the OLE DB Provider for ODBC with the SQL Server ODBC
driver and using the OLE DB Provider for SQL Server. The following steps
demonstrate the OpenSchema Method.
'Open the proper connection.
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Command1_Click()
'Getting the information about the columns in a particular table.
Set rs = cn.OpenSchema(adSchemaColumns, Array("pubs", "dbo", _
"authors"))
While Not rs.EOF
Debug.Print rs!COLUMN_NAME
rs.MoveNext
Wend
End Sub
Private Sub Command2_Click()
'Getting the information about the primary key for a table.
Set rs = cn.OpenSchema(adSchemaPrimaryKeys, Array("pubs", "dbo", _
"authors"))
MsgBox rs!COLUMN_NAME
End Sub
Private Sub Command3_Click()
'Getting the information about all the tables.
Dim criteria(3) As Variant
criteria(0) = "pubs"
criteria(1) = Empty
criteria(2) = Empty
criteria(3) = "table"
Set rs = cn.OpenSchema(adSchemaTables, criteria)
While Not rs.EOF
Debug.Print rs!TABLE_NAME
rs.MoveNext
Wend
End Sub
Private Sub Form_Load()
cn.Open "dsn=pubs;uid=<username>;pwd=<strong password>;"
'To test with the Native Provider for SQL Server, comment the
' line above then uncomment the following line. Modify to use
' your server.
'cn.Open "Provider=SQLOLEDB;Data Source=<servername>;" & _
' "User ID=sa;password=;"
End Sub
Run. Click each Command button to test. End.Modify the Form Load event
procedure to use the Native Provider for SQL Server. Again test. More
information on querytype and Criteria is available in the ADO documentation.
The schema information specified in OLE DB is based upon the assumption that
the provider supports the concept of a catalog and schema. 182831 How To Using the ADO OpenSchema Method from Visual C++
185979 How To Use ADO to Retrieve Table Index Information
Additional query words: adoobj
Keywords: kbdatabase kbhowto kbjet kbprovider KB186246