Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MFC CDaoDatabase ODBC connection to .MDB file

Posted on 1998-10-06
8
Medium Priority
?
1,581 Views
Last Modified: 2012-06-21
I am using the CDaoDatabase class to create and open an empty .MDB file.  I then create a CDaoTableDef object and try to create a connection to another .MDB file using ODBC.  I get errors in the create and append.  Any ideas? Here's my code :

CSPSDataAccess TestData; // derived from CObject
TestData.OpenMDB("d:\\recipes\\test.mdb"); // has a member variable m_DatabaseFile pointing to the CDaoDatabase object

CDaoTableDef* pODBCTable = new CDaoTableDef(&TestData.m_DatabaseFile);
pODBCTable->Create("TEST", FALSE, FALSE, "ODBC;DSN=TEST32;");
pODBCTable->Append();
CDaoRecordset Records(&TestData.m_DatabaseFile);
0
Comment
Question by:blevine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Author Comment

by:blevine
ID: 1174578
The error message is "No field defined - cannot append tabledef"
0
 

Expert Comment

by:mmachie
ID: 1174579
i know this sounds daft but, did you try creating a field?  
CDaoTableDef::CreateField().

mikem
0
 
LVL 86

Expert Comment

by:jkr
ID: 1174580
mmachie is right. In order to append a tabledef, you have to create fields first, e.g.

        CDaoTableDef*  ptd;

        // ...

        CDaoFieldInfo* pdfi = new CDaoFieldInfo();

                pdfi->m_nOrdinalPosition    =   0;
                pdfi->m_strSourceField      =   _T  (   "");
                pdfi->m_strSourceTable      =   _T  (   "");

                ptd->CreateField    (   *pdfi);

        // ...

        ptd->Append ();

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:blevine
ID: 1174581
No, what I am trying to do is open an empty .MDB file and connect to an existing .MDB file which may be on my machine or on a network drive.  I then want to determine what the table info is in .MDB file I connected to.  I do not know what the format is in that file.  I then want to extract the table information from the connected file and created table defs in the empty original .MDB file.  The documentation says that I should be able to connect through ODBC to the existing MDB file and use it as part of my tables.
0
 
LVL 8

Expert Comment

by:MaDdUCK
ID: 1174582
what's the DAO error code (obtain it from the dbDaoErrors object)...
0
 
LVL 3

Expert Comment

by:NullTerminator
ID: 1174583
You can't use CDao anything to talk to an access database through ODBC because the ODBC driver uses DAO to talk to the mdb.  Use CDatabase and CRecordset if you need to use ODBC,  otherwise, open it directly with DAO.  Create a seperate workspace if you need to.

Sorry
'\0'
0
 

Author Comment

by:blevine
ID: 1174584
Ok that sounds good.  I really need to attach to the table without opening it directly because I will be dealing with a huge amount of data.  Any samples on attaching and accessing record sets would be appreciated.  Also, how do I attach to SQL Server files using CDatabase?????
0
 
LVL 3

Accepted Solution

by:
NullTerminator earned 400 total points
ID: 1174585
this is the form of an odbc connect string
"ODBC;DSN=SALESDB;UID=Administrator;PWD=HighWire"

CDatabase myDB;
myDB.Open( LPCTSTR lpszDSN, BOOL bExclusive = FALSE, BOOL bReadOnly = FALSE,  PUT_YOUR_ODBC_CONNECT_HERE, BOOL bUseCursorLib = TRUE );
 
// sample i Have use to connect to SQL 6.x

if(m_bUseODBC) {
            CString strConnect;
            strConnect.Format("ODBC;DSN=%s;UID=%s;PWD=%s",
                m_strConnect, m_strUser, m_strPwd);
           if(_debug)  MessageBox("Prepare for odbc " + strConnect);
            m_pDB->Open("", FALSE, FALSE, strConnect);                
            }

If you want to work through an attached file via Access, create a sytem DSN ODBC datasource pointing to your SQL database.  Open an existing or new db.
Go to File | Get External Data | Link Tables.  Select ODBC datasources from file types,  pick your Datasource,  pick the tables you want to link,  rename them after they are linked if you like, they will stay linked.

This works for all sorts of data,  AS 400,  DB2, etc.

'\0'
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question