Working with tables in ADO is similar to working with them in DAO. Instead of working with the TableDefs collection, you will be working with the Tables collection.
The Tables collection is located off the Catalog object. One difference between ADO's Tables collection and DAO's TableDefs collection is that more than just standard table and link table objects are stored in it. Table 6.4 lists the object types in the ADO Tables collection.
Object Type | Description |
---|---|
ACCESS TABLE | Access system table. |
LINK | Linked table from a non-ODBC data source. |
PASS-THROUGH | The table is a linked table from an ODBC data source. |
SYSTEM TABLE | Jet system table. |
TABLE | Table. |
VIEW | Row returning, non-parameterized query. |
You can use a couple of methods to see what objects are in the Tables collection for a specific type. You can use the traditional DAO method by using the Tables collection and looking at the Table object's Type property. You can see this method used in Listing 6.6.
Sub ListTablesUsingTableCollection(strTypeToList As String) Dim catCurr As New ADOX.Catalog Dim tblCurr As ADOX.Table catCurr.ActiveConnection = CurrentProject.Connection For Each tblCurr In catCurr.Tables If tblCurr.Type = strTypeToList Then Debug.Print tblCurr.Name End If Next End Sub |
The other method available to ADO is using the OpenSchema method off the Connection object. This method is actually faster because you don't have to create the individual Table variables.
Creating tables in ADO is very much like creating them in DAO, where you append objects to the various necessary collections. Unlike DAO, you don't have to use the CreateObject() methods. Another difference is the use of the Keys collection, which is different from the Indexes collection in that it specifies a table's primary and foreign keys. Listing 6.8 shows the creation of a table, along with fields and the primary key field.
Sub CreateTableWIndexes() Dim catCurr As New ADOX.Catalog Dim tblNew As New ADOX.Table Dim keyNew As New ADOX.Key catCurr.ActiveConnection = CurrentProject.Connection ' Create a new Table object. With tblNew .Name = "MyNewTable" '-- Add new columns '- Add an autoincrement field .Columns.Append "MyKeyField", adInteger .Columns("MyKeyField").ParentCatalog = catCurr .Columns("MyKeyField").Properties("AutoIncrement") = True '-- Add a character field .Columns.Append "MyCharField", adWChar '-- Add a Memo field .Columns.Append "MyMemoField", adLongVarWChar .Columns("MyMemoField").Attributes = adColNullable '-- Add a Primary Key field keyNew.Name = "PrimaryKey" keyNew.Columns.Append "MyKeyField" .Keys.Append keyNew, adKeyPrimary End With ' Add the new table to the database. catCurr.Tables.Append tblNew Set catCurr = Nothing End Sub |
Look at Listing 6.8 step by step:
1. |
Set the catalog's connection, and then specify the name of the new table. As with DAO tables, the table won't actually be added to the Tables collection until the Append method is specified later. catCurr.ActiveConnection = CurrentProject.Connection ' Create a new Table object. With tblNew .Name = "MyNewTable" |
2. |
Add the columns to the new table: '- Add an autoincrement field .Columns.Append "MyKeyField", adInteger .Columns("MyKeyField").ParentCatalog = catCurr .Columns("MyKeyField").Properties("AutoIncrement") = True '-- Add a character field .Columns.Append "MyCharField", adWChar '-- Add a Memo field .Columns.Append "MyMemoField", adLongVarWChar .Columns("MyMemoField").Attributes = adColNullable In this case, three different data types have been added to the table: Long Integer (adInteger for ADO), Character, and Memo. Table 6.5 contains the various data types possible. |
3. |
Next, add the primary key to the table: '-- Add a Primary Key field keyNew.Name = "PrimaryKey" keyNew.Columns.Append "MyKeyField" .Keys.Append keyNew, adKeyPrimary |
4. |
Lastly, append the new table to the Tables collection: ' Add the new table to the database. catCurr.Tables.Append tblNew |
Now look at how easy it is to modify the table just created and to add an index.
To modify the table, you will just refer to it in the Tables collection. To add an index, you will create the new index and add it to the Indexes collection.
Sub ModifyTableAndAddIndex() Dim catCurr As New ADOX.Catalog Dim idxNew As New ADOX.Index catCurr.ActiveConnection = CurrentProject.Connection With catCurr.Tables("MyNewTable") idxNew.Name = "MyCharField" idxNew.Columns.Append "MyCharField" .Indexes.Append idxNew End With End Sub
These are just a few of the many things you can do by using ADO version 2.1, so get playing with them, and explore all the many other collections and objects in ADO.
18.191.215.117