Microsoft Access 2010 : SCHEMA RECORDSETS WITH ADO

Another useful feature of ADO is that it allows the creation of schema Recordsets. Schema Recordsets are Recordset objects that contain information about the data sources’ tables and views. This information can also be procured using ADOX, which is designed specifically for this task, but some details are more readily accessed using ADO schema Recordsets.

1. ADO Schema Recordsets

To open a schema Recordset, call the OpenSchema method from the Connection object. The OpenSchema method takes three parameters: Schema, Restrictions, and SchemaID. The following example function returns the schema for the tables in the current database:

Public Function OpenSchemaRecordset() As ADODB.Recordset

    ' Return Schema for the Tables in the current database
    Set CreateSchemaRecordset = _
            CurrentProject.Connection.OpenSchema(adSchemaTables)

End Function                                                  

2. Specifying Constraint Columns

To restrict the output of the OpenSchema method, supply an array of values from the restrictions list. In other words, where the preceding code returns a list of all the tables and views in the database, the constraint columns for the adSchemaTables option are: TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, and TABLE_TYPE. The array values must be specified in that order, so that the OpenSchema method handles them correctly:

Array(TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE)

To restrict the output to a single table with a TABLE_NAME of Contacts and a TABLE_TYPE of Table, the resulting array would be:

Array(TABLE_CATALOG, TABLE_SCHEMA, "Contacts", "Table")

So, the final code would be written as follows:

Public Function OpenSchemaWithConstraints() As ADODB.Recordset

    ' Return Schema for the "Contacts" table only
    Set OpenSchemaWithConstraints = _
            CurrentProject.Connection.OpenSchema( _
                adSchemaTables, _
                Array(Empty, Empty, "Contacts", "Table"))

End Function                                                         

 

This code would produce a Recordset object containing information about the properties of the Contacts table.

3. Using ACE Specific Schemas

The ACE provider also supplies eight provider-specific schema Recordset objects. The following is a list of each and their GUIDs:

'Access object security GUIDs
Public Const JET_SECURITY_FORMS = _
"{c49c842e-9dcb-11d1-9f0a-00c04fc2c2e0}"
Public Const JET_SECURITY_REPORTS = _
"{c49c8430-9dcb-11d1-9f0a-00c04fc2c2e0}"
Public Const JET_SECURITY_MACROS = _
"{c49c842f-9dcb-11d1-9f0a-00c04fc2c2e0}"

 

 

Public Const JET_SECURITY_MODULES = _
"{c49c8432-9dcb-11d1-9f0a-00c04fc2c2e0}"

'Jet OLE DB provider-defined schema rowsets
Public Const JET_SCHEMA_REPLPARTIALFILTERLIST = _
"{e2082df0-54ac-11d1-bdbb-00c04fb92675}"
Public Const JET_SCHEMA_REPLCONFLICTTABLES = _
"{e2082df2-54ac-11d1-bdbb-00c04fb92675}"
Public Const JET_SCHEMA_USERROSTER = _
"{947bb102-5d43-11d1-bdbf-00c04fb92675}"
Public Const JET_SCHEMA_ISAMSTATS = _
"{8703b612-5d43-11d1-bdbf-00c04fb92675}"

 

Interestingly enough, the JET_SCHEMA_USERROSTER option provides a Recordset containing a list of all of the users logged on. The following code illustrates an example of how to get this list of users:

Public Function WhoIsLoggedOn() As ADODB.Recordset

    ' Create the list of users logged on in a Recordset
    Set WhoIsLoggedOn = _
            CurrentProject.Connection.OpenSchema( _
                adSchemaProviderSpecific, , _
                "{947bb102-5d43-11d1-bdbf-00c04fb92675}") 'JET_SCHEMA_USERROSTER

End Function