Solved

Question in creating database?

Posted on 1998-09-26
4
273 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pressing cancel button with C# UI Automation 3 90
substring method in java 1 135
Detect file exist or not 3 198
Problem to open Excel file 15 212
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

679 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