Solved

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

Posted on 2000-02-27
11
220 Views
Last Modified: 2013-11-20


 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!
0
Comment
Question by:greenbird
  • 5
  • 2
  • 2
  • +1
11 Comments
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2563665
Can you please show the source code for you createNewTableDef function.

I assume it is your own function.
0
 

Author Comment

by:greenbird
ID: 2563829
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2563835
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
 

Author Comment

by:greenbird
ID: 2563841
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
 

Author Comment

by:greenbird
ID: 2563845
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:hiap_ho
ID: 2564069
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
 
LVL 1

Expert Comment

by:hiap_ho
ID: 2564072
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
 
LVL 3

Accepted Solution

by:
V_Bapat earned 10 total points
ID: 2564325
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
 

Author Comment

by:greenbird
ID: 2567705
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
 
LVL 3

Expert Comment

by:V_Bapat
ID: 2567884
I could not understand what you are saying. Please elaborate.
0
 

Author Comment

by:greenbird
ID: 2568301
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video discusses moving either the default database or any database to a new volume.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now