Home | Overview | How Do I | FAQ | Sample | Tutorial | ODBC Driver List
This article explains how to use CDaoTableDef objects. Topics covered include:
Creating a tabledef creates a new table in the target database. You create the tabledef and add fields (and possibly indexes) to it. The new table doesn't contain any data until you either add records from Microsoft Access (for a Microsoft Jet (.MDB) database) or create a recordset that adds records to the table.
Creating a new MFC CDaoTableDef object creates the underlying DAO tabledef object.
To create a tabledef
Tip The easiest way to create a tabledef is to create it in Microsoft Access. Open the target database, create tables, and save them in the database. Then you can use the tabledefs from your application’s code.
If you want to examine or manipulate the structure of an existing table, open an MFC tabledef object based on the DAO tabledef object stored in the database's TableDefs collection. Objects in the TableDefs collection are accessed by the user-defined name specified when the tabledef was created and appended to the collection.
To open a tabledef for an existing table
For examples, see the MFC Database sample DAOVIEW.
The following code from the LISTVIEW.CPP file in DAOVIEW illustrates opening a tabledef to get information about its fields and then add the field information to a list control:
// db is an open CDaoDatabase object
// strTableName is the user-defined name of the tabledef to open
CDaoTableDef td( &db );
try
{
td.Open( strTableName );
short nFields = td.GetFieldCount( );
for( short i=0; i < nFields; i++ )
{
td.GetFieldInfo(i,fieldInfo);
m_ctlList.AddColumn(fieldInfo.m_strName,i);
}
}
catch( CDaoException* e )
{
// Do nothing. Used to catch security violations opening tables.
e->Delete( );
}
td.Close( );
Unless a table is in an external data source, you can create table-type recordsets based on the table, in two ways:
A table-type recordset represents a base table (a table in a Microsoft Jet (.MDB) database) in code. You can’t open a table-type recordset on an ODBC database or on an attached table, but you can open one on an ISAM database opened directly, such as a FoxPro, dBASE or Paradox database.
You can use a table-type recordset to examine, add, change, or delete records in a single base table. You can’t use an SQL statement to filter or sort data as you can with dynaset-type and snapshot-type recordsets. This means you get all records, but table-type recordsets behave somewhat like dynaset-type recordsets in that only the current record is loaded into memory. When you move to a new record, it is loaded. Sorting is based on a predefined index. Table-type recordsets support bi-directional scrolling.
For more information, see class CDaoRecordset in the Class Library Reference and see the following topics in DAO Help:
See Also DAO: Where Is..., DAO Recordset, DAO External: Working with External Data Sources, DAO External: Creating an External Table