Problem creating new DB with MFC and DAO

Problem creating new DB with MFC and DAO

Post by Simon Culverhous » Tue, 04 Feb 1997 04:00:00



Hi,

I have tried to look through as many existing messages on these news-group
to find an answer to my problem, but as I came up blank I have a problem
which I hope someone can help me with.  I need to create a new database
(.mdb) file from within an MFC app and set up the tables, and am trying to
use DAO to do this.  I have trawled both the VC++ books, etc. and the MSDN
CD to find an answer.  I am rather suprised that more people would not want
to do this, and that there was no example on either the VC++ CD or MSDN.

I am using VC 4.2 and NT 4 - I also have MS-Office 95 installed (including
the Access application).  Below is a sample of the code I have written,
which is failing:

   CDaoDatabase *pDB;
   CDaoTableDef *pRootTable;
   CDaoRecordset *pExistingRecords;
   CDaoTableDefInfo tdiInfo;

   pDB = new CDaoDatabase;
   pDB->Create("D:\\MYDB.MDB");  // I have made sure that this file does
not exist before running

   // create a table def for the root table and try to open it
   pRootTable = new CDaoTableDef(pDB);

   // create a new table as we can't find an existing one
   pRootTable->Create("Test");
   pRootTable->SetName("Test");  // I have tried with and without this line

   // !!!!!!  This next list causes an exception.  It is an unknown
exception (error: 0x80000003)
   pRootTable->Append();   // ERROR ON THIS LINE

   // create the required fields
   pRootTable->CreateField("Index", dbInteger, 0, dbAutoIncrField);
   pRootTable->CreateField("GroupName", dbText, 16);
   pRootTable->CreateField("GroupType", dbByte, 0);
   pRootTable->CreateField("GroupUnits", dbText, 16);

   pRootTable->Close();
   pDB->Close();

   delete pRootTable;
   delete pDB;

I have tried to get to the bottom of this using the de*, but obviously
can't trace into the OLE automation call to the JET code.  All I know is
that the call to Append() caused an exception.  Does anyone have any ideas
as to the problem here ?  Any help really would be appreciated.

Thanks,
Simon.

--
Simon Culverhouse
Senior Software Engineer
Instron Ltd.


-- My opinions do not necessarily
reflect those of my kind and friendly
employer (so there !) *8) --

 
 
 

Problem creating new DB with MFC and DAO

Post by Steve Zimmerma » Tue, 04 Feb 1997 04:00:00


I've done this successfully before. Off the top of my head, I'd say that
your problem is that you are calling Append before creating any fields.
Make the calls to CreateField before calling Append and it should work. If
it doesn't, let us know.

--

"What prodigies of human inventiveness [are] being directed
 to reading each other's mail!"--Carl Sagan, Contact



Quote:> I have tried to look through as many existing messages on these
news-group
> to find an answer to my problem, but as I came up blank I have a problem
> which I hope someone can help me with.  I need to create a new database
> (.mdb) file from within an MFC app and set up the tables, and am trying
to
> use DAO to do this.  I have trawled both the VC++ books, etc. and the
MSDN
> CD to find an answer.  I am rather suprised that more people would not
want
> to do this, and that there was no example on either the VC++ CD or MSDN.
>    CDaoDatabase *pDB;
>    CDaoTableDef *pRootTable;
>    CDaoRecordset *pExistingRecords;
>    CDaoTableDefInfo tdiInfo;
>    pDB = new CDaoDatabase;
>    pDB->Create("D:\\MYDB.MDB");  // I have made sure that this file does
> not exist before running

>    // create a table def for the root table and try to open it
>    pRootTable = new CDaoTableDef(pDB);

>    // create a new table as we can't find an existing one
>    pRootTable->Create("Test");
>    pRootTable->SetName("Test");  // I have tried with and without this
line

>    // !!!!!!  This next list causes an exception.  It is an unknown
> exception (error: 0x80000003)
>    pRootTable->Append();   // ERROR ON THIS LINE

>    // create the required fields
>    pRootTable->CreateField("Index", dbInteger, 0, dbAutoIncrField);
>    pRootTable->CreateField("GroupName", dbText, 16);
>    pRootTable->CreateField("GroupType", dbByte, 0);
>    pRootTable->CreateField("GroupUnits", dbText, 16);

>    pRootTable->Close();
>    pDB->Close();

>    delete pRootTable;
>    delete pDB;


 
 
 

Problem creating new DB with MFC and DAO

Post by Arno Carigie » Thu, 06 Feb 1997 04:00:00


Hi Simon

have you ever looked/searched in the FTP Softlib from MS?
There's samples of DAO.
Perhaps the sample
06-14-96        S15690  daotbl.exe
solves your problem?

cu arno