DAO Tabledef: Using Tabledefs

Visual Studio 6.0

https://msdn.microsoft.com/en-us/library/aa240996(v=vs.60).aspxOverviewHow Do IFAQSamplehttps://msdn.microsoft.com/en-us/library/aa716481(v=vs.60).aspxODBC Driver List

This article explains how to use objects. Topics covered include:

Creating a Tabledef

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 object creates the underlying DAO tabledef object.

To create a tabledef

  1. Construct a CDaoTableDef object, supplying a pointer to the object to which the tabledef will belong.

  2. Call the tabledef object's member function.

  3. Set any of the tabledef object’s properties that you want. Call the , , , , , or member functions.

  4. Add fields to the tabledef by calling its member function for each field. (You can’t modify the schema of an attached table, so this step applies only to local base tables and tables in external data sources that you open directly.)

  5. Optionally add indexes to the tabledef by calling its member function for each index.

  6. Call the tabledef object’s member function to save the tabledef in the database’s TableDefs collection.

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.

Opening an Existing Tabledef

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

  1. Construct a object, passing a pointer to the object to which the tabledef belongs.

  2. Call the tabledef object's member function, specifying the user-defined name of the tabledef saved in the TableDefs collection. The name may or may not be the same as the name of the underlying source table.

For examples, see the MFC Database sample https://msdn.microsoft.com/en-us/library/ms386428(v=vs.60).aspx .

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 );
    td.Open( strTableName );
    short nFields = td.GetFieldCount( );
    for( short i=0; i < nFields; i++ )
catch( CDaoException* e )
    // Do nothing. Used to catch security violations opening tables.
    e->Delete( );
td.Close( );

Creating a Table-Type Recordset

Unless a table is in an external data source, you can create table-type recordsets based on the table, in two ways:

  • Create a tabledef, then create a recordset from the tabledef.

  • Create a recordset and specify dbOpenTable in the nOpenType parameter to .

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 in the Class Library Reference and see the following topics in DAO Help:

  • Recordset Object

  • Table-Type Recordset Object

See Also   DAO: Where Is..., DAO Recordset, DAO External: Working with External Data Sources, DAO External: Creating an External Table