HELP! how to create a new table in .mdb file?



 after i create a new database (.mdb), i wnat to create a table in it, after that i want to add new fields and fill them with values... however it always say the table does not exist. i
 use the code:

 extern BOOL createNewTableDef(CDaoDatabase * pDatabase, CDaoTableDef **ppTableDef,
                                  CString strTableName);
 if (!createDatabase(m_ppDatabase, m_strDatabaseName))
         (*m_ppDatabase) = NULL;
 m_strTableName="DataTab";
 createNewTableDef((*m_ppDatabase), &m_ppTableDef, m_strTableName);



 how come???

 thanks a lot!
greenbirdAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
V_BapatConnect With a Mentor Commented:
What ever you are doing is correct. The problem is occuring somewhere else.

You have to call CDaoTableDef::Append() to save the table by appending it to the database’s TableDefs collection. Create puts the tabledef into an open state, so after calling Create you do not call Open.

The code for createNewTableDef() you posted earlier is from MSDN sample. There is a function appendTableDef() in the same cpp file. You can use that and call after creating the table.

Alternatively, call CDaoTableDef::Append().

This is a code snippet where I create a table:

            if(CTableDef::CreateTableDef(m_pDatabase, &m_pTable, strTableName[nTableIdx]))
            {
                  InitializeFields(TRUE); // Initialize all the fields
                  AddNewField(strFieldName[count], lType[count], lSize[count]);
            CTableDef::AppendTableDef(m_pDatabase, m_pTable);
            }


Vicky
0
 
RONSLOWCommented:
Can you please show the source code for you createNewTableDef function.

I assume it is your own function.
0
 
greenbirdAuthor Commented:
Hi,

the code is as folliwng:

BOOL createNewTableDef(CDaoDatabase * pDatabase, CDaoTableDef **ppTableDef,
CString strTableName)
{
// if the database is non-existent, then the // answer is obvious
      if (pDatabase == NULL)
            return FALSE;

// check for existing table with this name just to //be safe
if (IsExistentTable(pDatabase, strTableName))
{
AfxMessageBox(_T("A table by that name already exists."));

// return FALSE since can't create duplicate table
      return FALSE;
}

// initialize creation failure indicator
BOOL bCreateFailed = FALSE;

// construct tabledef
*ppTableDef = new CDaoTableDef(pDatabase);

// failed to allocate so exit
if ((*ppTableDef) == NULL)
 return FALSE;

// no duplication, so create the tabledef if possible
TRY
{
            (*ppTableDef)->Create(strTableName,dbAttachExclusive);
}
CATCH (CDaoException, e)
{
// construct a meaningful message
CString strMessage = _T("Couldn't create tabledef--Exception: ");
strMessage += e->m_pErrorInfo->m_strDescription;

AfxMessageBox(strMessage);

// indicate failure
bCreateFailed = TRUE;

// delete the tabledef on failure
if (*ppTableDef != NULL)
{
 delete *ppTableDef;
*ppTableDef = NULL;
}
}
AND_CATCH (CMemoryException, e)
{
// output status
AfxMessageBox(_T("Failed to create tabledef--Memory exception thrown."));

// indicate failure
bCreateFailed = TRUE;

// delete the tabledef on failure
if (*ppTableDef != NULL)
{
delete *ppTableDef;
*ppTableDef = NULL;
}
}
END_CATCH

// return TRUE if creation succeeds
return (!bCreateFailed);
}

thank u!
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
RONSLOWCommented:
What do you mean by "however it always say the table does not exist"

Do you mean the return value is false?  Or does some other function tell you it is not there?

And in addition, does it actaully get created in the MDB when you look at it?
0
 
greenbirdAuthor Commented:
hi,

no, i check the database file, the table is not there, that is, there is no table created.. an empty dabtabase file with no tables... but the createNewTableDef is successful...
so how? thanksa  lot!
0
 
greenbirdAuthor Commented:
or i want to create a table then add new fields to it with values... i do it this way: (without using the createNewTableDef , directly use the folllowing)

strcpy(sql_add,"CREATE TABLE DataTab (");
strcat(sql_add,field_name);

strcat(sql_add," CHAR(10))");
      (*m_ppDatabase)->Execute(sql_add);//"ALTER TABLE Table1 ADD A4 CHAR(3)");       
(*m_ppDatabase)->Close();

but it is ok for adding one field, if i want to add more fields, then it says the table exist already.

then i want to change it to:
if the table already exist, then use ALTER TABLE ...
so do u know how to check whether a table already exist?

could u pls provide some code for doing so? coz i am new to VC++ and not much time left for my project now... :(

Pls help!!!! thanks a lot!
0
 
hiap_hoCommented:
This is how I usually create the database from scratch

#include "afxdao.h"
      CDaoDatabase db;
      db.Create("c:\\mydb.mdb",dbLangGeneral,dbVersion30);
      CDaoTableDef td(&db);
      td.Create("TableName",dbAttachExclusive,NULL,NULL);
      td.CreateField("First Field",dbLong,4,dbUpdatableField);
      td.CreateField("Second Field",dbText,20,dbVariableField);
      td.Append();
      td.Close();
        db.Close();

If you need further explanation, I'll be happy to help. Hope this will help

0
 
hiap_hoCommented:
if you want to add some more field later on, you just need to use td.Open(bla,bla,bla) <--- see the complete syntax in MSDN. Then use td.CreateField(blablabla).
Afterthat
td.Close();
db.Close();

Give out more points then you'll get more help... from everybody
0
 
greenbirdAuthor Commented:
Hi, all,

thanks... i solved it... but there is still one problem:

say i want to insert 3 fields into this new database, it works. but later when i check the database file, it seems the data are not inserted seqwuencially. that is , say 1000 tuples of field "a" are added, then later 200 tuples of "b" are added, then 500 tuples of field "a" are added... how to solve it? thanks a lot!
0
 
V_BapatCommented:
I could not understand what you are saying. Please elaborate.
0
 
greenbirdAuthor Commented:
Hi,

ok lets say i want to insert 2 fields "a" and "b"  to the new database, but when i check the database, it is like this:

the values of the fields are got from an old database, say there are 2000 records in the original db.. then the new one is like this:

for the first 1000 records , only values of field "a" are added,
for the next 1000 records, only values of field "b" are added..
for the next 1000 records, only values of field "a" are added...
for the next 1000 records, only values of fiedl "b" are added...

so it seemes that the values of the new one are created parallely, fill some for field "a", then for field "b".... not fill in "a" (since it is the first field i want to insert), then fill in "b"... so i do not know what is wrong ?

pls help! as i need to solve it asap....

thanks a lot!
0
All Courses

From novice to tech pro — start learning today.