HOWTO: Use ADO to Retrieve Table Index InformationID: Q185979
|
If you need to retrieve index information for a table using ActiveX Data Objects (ADO) you must use a custom stored procedure or the ADO OpenSchema method. The ADO OpenSchema method simplifies access to the indexes of a table and provides a universal way to retrieve that information. The following sample code shows how to retrieve index information for a specific table using the OpenSchema method.
To see how this method works, follow these steps:
Dim cn As ADODB.Connection
Dim rsSchema As ADODB.Recordset
Dim fld As ADODB.Field
Dim rCriteria As Variant
Set cn = New ADODB.Connection
With cn
.Provider = "MSDASQL" 'default Provider=MSDASQL
.CursorLocation = adUseServer
.ConnectionString = "driver={SQL Server};server=(local);" & _
"uid=sa;pwd=;database=pubs"
.Open
End With
'Pass in the table name to retrieve index info. The other
'array parameters may be defined as follows:
' TABLE_CATALOG (first parameter)
' TABLE_SCHEMA (second)
' INDEX_NAME (third)
' TYPE (fourth)
' TABLE_NAME (fifth, e.g. "employee")
rCriteria = Array(Empty, Empty, Empty, Empty, "employee")
Set rsSchema = cn.OpenSchema(adSchemaIndexes, rCriteria)
Debug.Print rsSchema.RecordCount
While Not rsSchema.EOF
For Each fld In rsSchema.Fields
Debug.Print fld.Name
Debug.Print fld.Value
Debug.Print "------------------------------------------------"
Next
rsSchema.MoveNext
Wend
rsSchema.Close
Set rsSchema = Nothing
cn.Close
Set cn = Nothing
Set fld = Nothing
Microsoft Developer Network, search on: "OpenSchema Method - ADO"
Additional query words: kbDSupport kbdse
Keywords : kbADO kbADO150 kbADO200
Version : WINDOWS:1.5,2.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: April 20, 1999