Question in creating database?

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!!!
kkuuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

umaramaCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kkuuAuthor Commented:
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
umaramaCommented:
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
umaramaCommented:
I am wondering if your question is answered ? If not please feel free to comment !

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.