HOWTO: Use the ADO OpenSchema Method in Visual BasicID: Q186246
|
This article describes how to use the OpenSchema method of the ActiveX Data
Objects (ADO) Connection object to obtain more information about the
database or table.
The syntax for OpenSchema is below:
Set recordset = connection.OpenSchema (QueryType, Criteria, SchemaID)
Here are the three parameters for the OpenSchema method:
http://www.microsoft.com/data/ado/
QueryType Criteria
=============================
adSchemaTables TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
TABLE_TYPE
The ODBC driver for Microsoft Access treats catalog as the database and
schema as owner. The ODBC driver for SQL server also follows the same
terminology. The criteria Array member indicates the string values to
restrict the query results. The number of the array members varies
depending on the querytype.
For example, TABLE_CATALOG is pubs, TABLE_SCHEMA is "dbo", TABLE_NAME is
"authors". Similarly, one has to substitute the column name in case of
following querytype adSchemaColumns.
QueryType Criteria
===============================
adSchemaColumns TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
You provide the index name in case of adSchemaIndexes querytype.
QueryType Criteria
================================
adSchemaIndexes TABLE_CATALOG
TABLE_SCHEMA
INDEX_NAME
TYPE
TABLE_NAME
The most important thing to remember is that the order of providing the
values has to be the same.
'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", Null))
MsgBox rs(0)
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(3).Value
End Sub
Private Sub Command3_Click()
'Getting the information about all the tables.
Dim criteria(3) As Variant
criteria(0) = "pubs"
criteria(1) = Null
criteria(2) = Null
criteria(3) = "table"
Set rs = cn.OpenSchema(adSchemaTables, criteria)
While Not rs.EOF
Debug.Print CStr(rs(2).Value)
rs.MoveNext
Wend
End Sub
Private Sub Form_Load()
cn.Open "dsn=pubs;uid=sa;pwd=;"
End Sub
Additional query words: adoobj kbDSupport kbdse
Keywords : kbDSupport
Version : WINDOWS:1.5,2.0,5.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: May 24, 1999