Solved

Question in creating database?

Posted on 1998-09-26
4
270 Views
Last Modified: 2013-11-20
Dear Sir,

I have used the following code to create the database.

CDaoFieldInfo m_FI;
CDaoIndexInfo m_II;      

void CMyFormView::OnButton6()
{
        CDaoDatabase *ProDatabase;
        CDaoTableDef *ProTableDef;
      CDaoDatabase **ppDatabase=&ProDatabase;
      *ppDatabase = new CDaoDatabase;
      //create the database
      try
      {
            (*ppDatabase)->Create("testfu1.mdb", dbLangGeneral ,dbVersion30);
      }
      catch (CDaoException *e)
      {
            CString message = _T("Couldn't create database--Exception: ");                
            message += e->m_pErrorInfo->m_strDescription;                             
            AfxMessageBox(message);                                                                   
            e->Delete();
            delete *ppDatabase;
            *ppDatabase = NULL;
            
      }
      catch (CMemoryException *e)
      {
            AfxMessageBox(_T("Failed to create database--Memory exception thrown."));  
            e->Delete();
            delete *ppDatabase;
            *ppDatabase = NULL;
      }

      CString m_strTableName = "Users";
      CDaoTableDef **ppTableDef = &ProTableDef;
      *ppTableDef = new CDaoTableDef(ProDatabase);
      // so create the tabledef if possible
      TRY
      {                                                                                             
            (*ppTableDef)->Create(m_strTableName);                                                      
      }                                                                                             
      CATCH (CDaoException, e)
      {                                                                                             
            CString strMessage = _T("Couldn't create tabledef--Exception: ");                                
            strMessage += e->m_pErrorInfo->m_strDescription;                                             
            AfxMessageBox(strMessage);
            // delete the tabledef on failure                                                      
            if (*ppTableDef != NULL)                                                                  
            {                                                                                         
                  delete *ppTableDef;                                                                   
                  *ppTableDef = NULL;                                                                   
            }                                                                                         
      }                                                                                             
      AND_CATCH (CMemoryException, e)
      {
            AfxMessageBox(_T("Failed to create tabledef--Memory exception thrown."));                  
            if (*ppTableDef != NULL)                                                                  
            {                                                                                         
                  delete *ppTableDef;                                                                   
                  *ppTableDef = NULL;                                                                   
            }                                                                                         
      }
      END_CATCH                                                                                     

      // property of the fields
      m_FI.m_lSize=30;
      m_FI.m_lAttributes |= dbVariableField;
      m_FI.m_nOrdinalPosition = 0;
      m_FI.m_strName = _T("First Field");
      m_FI.m_nType = dbText;      
      m_FI.m_strDefaultValue = _T("");
      m_FI.m_bRequired = FALSE;
      m_FI.m_bAllowZeroLength = TRUE;
      m_FI.m_strValidationRule = _T("");
      m_FI.m_strValidationText = _T("");
      
      //create the fields
      ProTableDef->CreateField(m_FI);
      ProTableDef->Append();
      m_FI.m_strName = _T("Second Field");
      ProTableDef->CreateField(m_FI);
      m_FI.m_strName = _T("Third Field");
      ProTableDef->CreateField(m_FI);
      m_FI.m_strName = _T("Fourth Field");
      ProTableDef->CreateField(m_FI);
      
      m_II.m_strName = _T("First Field");
      m_II.m_bPrimary = FALSE;
      m_II.m_bIgnoreNulls = FALSE;
      m_II.m_bRequired = FALSE;
      m_II.m_bUnique = FALSE;

      if (m_II.m_pFieldInfos != NULL)
      {
            delete [] m_II.m_pFieldInfos;
            m_II.m_pFieldInfos = NULL;
            m_II.m_nFields = 0;
      }

      CDaoIndexFieldInfo **ppFields=&(m_II.m_pFieldInfos);      
      (*ppFields)=new CDaoIndexFieldInfo[1];

      (*ppFields)[0].m_strName="First Field";
      (*ppFields)[0].m_bDescending= FALSE;
      
      m_II.m_nFields=1;

      // try create index
      TRY                                                                   
      {                                                                     
            ProTableDef->CreateIndex(m_II);                            
      }                                                                     
      CATCH (CDaoException, e)                                              
      {                                                                     
                  CString message = _T("Couldn't create Index--Exception: ");           
            message += e->m_pErrorInfo->m_strDescription;
            
            AfxMessageBox(message);                                                      
      }                                                                     
      AND_CATCH (CMemoryException, e)
      {
            AfxMessageBox(_T("Failed to create Index--Memory exception thrown."));                                             
      }
      END_CATCH                                                             

      // TODO: Add your control notification handler code here
      
}

I don't know why the two variables :

CDaoFieldInfo m_FI;
CDaoIndexInfo m_II;      

must be declared outside the function as a global variable.

If I declare these two variables inside the above funtion
or in the *.h file. The error "Invalid argument" will
be appeared.

I found the statment "ProTableDef->Append();" may contain
problem. Because I do not execute this statment, the two
variables can be delcared inside the function.
Also, I am not sure is there any problem in my process
of creating the database.
If yes, please tell me what can I do to correct the error.
Sorry for giving a so long source code.

Thanks!!!
0
Comment
Question by:kkuu
  • 3
4 Comments
 

Accepted Solution

by:
umarama earned 70 total points
ID: 1322636
Your Problem lies with the following line

m_FI.m_lAttributes |= dbVariableField;

Correct it to read

m_FI.m_lAttributes = dbVariableField;

When your DaoFieldInfo is declared globally, all the members are initialized to zero. (That's the feature of global variables - they are initialized to zero). When you declared it locally, it was not gettting initialized to zero. You simply ORed it with dbVariableField and the resultant value it had was not meaningful for DAO to understand !

I hop0e it answers your problem.
0
 

Author Comment

by:kkuu
ID: 1322637
Dear umarama,

If I want to declare the two variables
1. CDaoFieldInfo m_FI;
2. CDaoIndexInfo m_II;  
locally, what can I do?
I don't understand your statment:
{When you declared it locally, it  was not gettting initialized to zero. You simply ORed it with dbVariableField and the resultant value it had was not meaningful for DAO to understand ! }

Can you describe more detail....or give the source code to do this?

Thanks
0
 

Expert Comment

by:umarama
ID: 1322638
You can declare your variables locally. In my answer, I was trying to explain as to why your code worked when your variables were global and not when they were local.

Let me clear my point before I answer your question. O.K. ?

Local variables are uninitialized when you declare them. They contain arbritrary values. Not defined.
Global variables are initialized to 0. A known value. Always.

e.g.
int gi; // declared global
Function()
{
 int i; // declared locally but not initialized
 int j = 1; // declared locally but initialized to 1
 ..
 int k = i; // k = some arbitrary value, say 17824.
 k = j; // k will be = 2
 k = gi; // k will be = 0 since gi is by default initialized to 0 as it was declared as a global variable.
}

So, in your code, you were using the following statement
m_FI.m_lAttributes |= dbVariableField;
// If you check in DevStudio\VC\include\DBDAOINT.H(75):    
// dbVariableField = 2. If DAO sees 2 in m_FI.m_lAttributes, it will understand you meant Variable Field option.

Case 1: m_FI was declared globally. The structure was initialized to zero, i.e. all it's elements were initialized to 0.

So, m_FI.m_lAttributes |= dbVariableField equated to
m_FI.m_lAttributes = m_FI.m_lAttributes(0) + dbVariableField(2)
                   = 2 WHICH IS CORRECT. DAO UNDERSTOOD YOUR OPTION AND YOUR CODE WORKED.

Case 2: m_FI was declared locally. The structure was NOT initialized to zero, i.e. all it's elements were having some arbitrary value.

So, m_FI.m_lAttributes |= dbVariableField equated to
m_FI.m_lAttributes = m_FI.m_lAttributes(some initial junk value) | (OR) dbVariableField(2)
                   = Junk Value WHICH IS NOT YOU WANTED.
AND YOUR CODE FAILED since DAO - CreateField & Append could not interpret that junk value.

Now, if you wish to declare your variables locally, please go ahead ! YOU CAN DO SO. Just make sure you __initialize__ the elements of m_FI properly. Hence I mentioned that you should not OR ( |= ) and instead initialize with = statement.
m_FI.m_lAttributes = dbVariableField ;

Your code will look something like this ..

Function()
{
   ....
   CDaoFieldInfo m_FI;
   CDaoIndexInfo m_II;

   ...
   // REMOVE THIS LINE m_FI.m_lAttributes |= dbVariableField ;
   m_FI.m_lAttributes = dbVariableField ;// ADD THIS LINE

   Rest of your code is correct. No need to change.
}

Make the above change and see your code working correctly !!

I hope to have clarified your doubts. If not, please comment. I will gladly clear them. All the best.
0
 

Expert Comment

by:umarama
ID: 1322639
I am wondering if your question is answered ? If not please feel free to comment !

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need Help INsttalling wget on Mavericks OS X 3 90
withoutTen challenge 14 99
Making an alias 7 79
JQuery serialize and unserialize 8 49
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

911 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

21 Experts available now in Live!

Get 1:1 Help Now