Home | Overview | How Do I | FAQ | Sample | Tutorial | ODBC Driver List
This article explains how to attach a table from an external data source, such as an ODBC data source, to your current Microsoft Jet (.MDB) database. Attaching external tables is generally more efficient than opening them directly, as explained in the article DAO External: Working with External Data Sources.
Important For performance reasons, it is best to attach tables in ODBC data sources rather than to open them directly. You can open non-ODBC external data sources directly if you like.
Tip If you attach a table from within Microsoft Access, you can then use the table from MFC.
To attach an external table using the MFC DAO classes
Construct a CDaoDatabase object, or obtain a pointer to one (from an open recordset object, for example) and call the object's Open member function.
In the Create call, you can specify the source table name and the connect string. Or you can accept the defaults in Create and separately call SetConnect and SetSourceTableName to specify the connect string and the name of the table as it appears on the data source. The example following this procedure calls SetConnect and SetSourceTableName.
Call the tabledef object's Append member function.
You can do the following, among other things:
The following example illustrates how to attach an external table:
// Construct the database and the tabledef
CDaoDatabase db;
db.Open("C:\\datatbase\\theDB.mdb");
// Construct the tabledef then create attached table
CDaoTableDef td( &db );
td.Create( "Preferred Customers", 0, "Customers",
"ODBC;DSN=afx;UID=sa;PWD=Fred" );
// Attach the tabledef to the external data source
td.Append( );
// Use td ...
The parameters to create are the tabledef name, attributes, source table name, and connect string.
Your link to the attached table remains active unless you delete the tabledef object or move the source table. If you move the source table, you can refresh the link using the tabledef object's RefreshLink member function.
Note For the external indexed sequential access method (ISAM) databases, such as FoxPro and dBASE, specify the full path to the directory in which the database files are located when a database name is called for.
Tip Because a tabledef object name can be any valid Microsoft Access table name, you can give the attached table a more descriptive name than is often allowed in the external data source. For example, if you attach an external dBASE table named SLSDATA, you can rename the attached table as “Sales Data 1995 (from dBASE).” The code in the previous example illustrates this.
See Also DAO: Where Is..., DAO External: Working with External Data Sources, DAO External: Opening External Databases Directly