HOWTO: Export and Import Access Tables Using DAO or ODBCID: Q200427
|
This article details how to import and export tables using SQL DML (data manipulation language) statements with the Microsoft Access ODBC driver.
The Microsoft Access ODBC driver can read and write data into Jet ISAM drivers as well as ODBC drivers using SQL statements. The driver understands a special SQL syntax that permits you to prefix a table name with any valid Jet connection string, which allows you to create SQL statements that move tables from one data source to another.
The basic concept behind the examples shown in this article is to open a Microsoft Access database and use the "SELECT ... INTO ... FROM" SQL statement. Internally, Jet performs the translations required for exporting or importing the data from the source and target data sources.
The general syntax of the "copy table" SQL is:
SELECT * INTO <Target> FROM <Source>
Where <Source> is the table we want to copy and <Target> is the destination for the table. Note that this SQL statement will attempt to create <Target> with the same table structure as <Source> and populate <Target> with all of the records from <Source>.
[<Full path to Microsoft Access database>].[<Table Name>]
[ODBC;<ODBC Connection String>].[<Table Name>]
[<ISAM Name>;<ISAM Connection String>].[<Table Name>]
Here are some valid syntax examples:
[c:\mydata\db1.mdb].[Customers]
[ODBC;DSN=MyODBCDSN;UID=sa;PWD=;].[authors]
[ODBC;Driver=SQL Server;Server=XXX;Database=Pubs;UID=sa;PWD=;].[authors]
[Excel 5.0;HDR=Yes;DATABASE=c:\book1.xls;].[Sheet1$]
For more information on creating valid Jet connection strings, see the following whitepaper:
#include <afxdao.h> // Needed for MFC DAO classes.
CDaoDatabase db;
CString SQL;
SQL = "SELECT * INTO "
"[Excel 8.0;HDR=Yes;DATABASE=c:\\customers.xls].[Sheet1] "
"FROM [Customers]";
try
{
// Open database and execute SQL statement to copy data.
db.Open( "c:\\nw97.mdb" );
db.Execute( SQL, dbFailOnError );
}
catch( CDaoException * pEX )
{
// Display errors.
AfxMessageBox( pEX->m_pErrorInfo->m_strDescription );
pEX->Delete();
}
#include <afxdao.h> // Needed for MFC DAO classes.
CDaoDatabase db;
CString SQL;
// Change XXX to the name of your SQL Server.
SQL = "SELECT * INTO "
"[LocalAuthors] "
"FROM "
"[ODBC;Driver=SQL Server;SERVER=XXX;DATABASE=Pubs;UID=sa;PWD=;]."
"[authors]";
try
{
// Open database and execute SQL statement to copy data.
db.Open( "c:\\nw97.mdb" );
db.Execute( SQL, dbFailOnError );
}
catch( CDaoException * pEX )
{
// Display errors.
AfxMessageBox( pEX->m_pErrorInfo->m_strDescription );
pEX->Delete();
}
#include <afxdb.h> // Needed for MFC ODBC classes.
CDatabase db;
CString SQL;
SQL = "SELECT * INTO "
"[Excel 8.0;HDR=Yes;DATABASE=c:\\customers.xls].[Sheet1] "
"FROM [Customers]";
try
{
// Open database and execute SQL statement to copy data.
db.OpenEx( "Driver=Microsoft Access Driver (*.mdb);"
"DBQ=c:\\nw97.mdb;", CDatabase::noOdbcDialog );
db.ExecuteSQL( SQL );
}
catch( CDBException* pEX )
{
// Display errors.
AfxMessageBox( pEX->m_strError );
pEX->Delete();
}
#include <afxdb.h> // Needed for MFC ODBC classes.
CDatabase db;
CString SQL;
// Change XXX to the name of your SQL Server.
SQL = "SELECT * INTO "
"[ODBC;Driver=SQL Server;SERVER=XXX;DATABASE=Pubs;UID=sa;PWD=;]."
"[RemoteShippers] "
"FROM [Shippers]";
try
{
// Open database and execute SQL statement to copy data.
db.OpenEx( "Driver=Microsoft Access Driver (*.mdb);"
"DBQ=c:\\nw97.mdb;", CDatabase::noOdbcDialog );
db.ExecuteSQL( SQL );
}
catch( CDBException* pEX )
{
// Display errors.
AfxMessageBox( pEX->m_strError );
pEX->Delete();
}
Additional query words:
Keywords : kbDatabase kbJET kbMDAC kbODBC kbVS600
Version : WINDOWS:2.0,6.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: March 20, 1999