Urgent help! CdaoDatabase add multiple new fields ...


I have created a new database as specified by user, then i want to insert new fields (together with values got from an old database). i use the following code:(outline)

// create the new database
if (!createDatabase(&m_newDatabase, m_newDatabaseName))
    m_newDatabase = NULL;

// read values from old one and add them to the new one
for (i=0;i<count;i++){

  // add/create  new fields
   openDatabase(m_newDatabase, m_strDatabaseName, FALSE);      
   strcpy(sql_add,"CREATE TABLE DataTab (");
   strcat(sql_add," CHAR(10))");

   openDatabase(m_newDatabase, m_newDatabaseName, FALSE);
  openTableDef(m_newDatabase,&m_newTableDef,                         m_strTableName);

  // open a recordset for the new database
  CDaoRecordset rs1(m_newDatabase);            

  //open a recordset for the old database
  openDatabase(&m_oldDatabase, m_oldDatabaseName, FALSE);
  openTableDef(m_oldDatabase, &m_oldTableDef, m_strTableName);
  CDaoRecordset rs(m_oldDatabase);            
 // read values from old one and add them to the new one


my problem is :

for the first time i add ONE new field, it works; but later on if i did not delete the newly created databse, and if i continue to add new filed(s) (even to another new database), it says:

the DataTab already exists...

i use the same table name for the old and new database...

any one has any idea? pls help me!!!! i need to solve it urgently!!!!

thanks a million!!!!

Who is Participating?
V_BapatConnect With a Mentor Commented:
This function copies all the tables. All the tables should already be created before calling this function.

So you have to write a function to create all the tables and create the fields.

      COleVariant covFieldValue;
      CString strTableName;
      int nTableCount, i, j, nFieldCount;
      CDaoTableDefInfo pTableInfo;
      CDaoDatabase* m_pSrcDatabase = NULL;
      CDatabase::OpenDatabase(&m_pSrcDatabase, m_strDbName);
      CDaoRecordset m_pSrc(m_pSrcDatabase);
      CDaoRecordset m_pDest(m_pDatabase);
      nTableCount = m_pDatabase->GetTableDefCount();
      for(j = 0; j < nTableCount; j++)
            m_pDatabase->GetTableDefInfo(j, pTableInfo);

            if(pTableInfo.m_lAttributes & dbSystemObject | pTableInfo.m_lAttributes & dbHiddenObject)

            m_strSQL = CString("SELECT * FROM ") + pTableInfo.m_strName;
            m_pSrc.Open(dbOpenDynaset, m_strSQL);
            m_pDest.Open(dbOpenDynaset, m_strSQL);
            nFieldCount = m_pSrc.GetFieldCount();
                  for(i = 0; i < nFieldCount; i++)
                        m_pSrc.GetFieldValue(i, covFieldValue);
                        m_pDest.SetFieldValue(i, covFieldValue);



Why don't you split the job into 2 parts?

1. Create tables & fields.
2. Copy records.

This will make it easy.

I have code to copy all the tables which I can give you.
greenbirdAuthor Commented:

where is ur code? :) u can email to :

i think in my another post, u answered my qn rite? i post new thing there, pls check it out...

thanks a lot for ur greate help! really appreciate it..
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

greenbirdAuthor Commented:
Hi, Vicky,

thanks a lot! i will try it tomorrow.. but could u pls refer to my another post: add field problem also, i have some qns there waiting for ur help... thanks a lot!
I was out of town for last 4 days. I will try it today.

What about this one. Did you try it?

greenbirdAuthor Commented:
Hi, V_bapat,

no wonder why u did not help me .. :) now feel good that u will help me :) i need to solve it by tomorrow....
Thanks a lot!
greenbirdAuthor Commented:
Adjusted points to 20
greenbirdAuthor Commented:

sovle it already :))) thanks a lot for ur help!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.