home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / database / daotable / field.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  6KB  |  218 lines

  1. // field.cpp : MFC DAO Field specific functions
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12. //
  13. // all the field specific functions:
  14. //
  15. //BOOL IsExistentField(CDaoTableDef *pTableDef, CString strFieldName);
  16. //BOOL createNewField(CDaoTableDef *pTableDef, CDaoFieldInfo *pFieldInfo);
  17. //BOOL getFieldInfo(CDaoTableDef *pTableDef, CDaoFieldInfo *pFieldInfo,
  18. //                int fieldIndex, BOOL bReportErrors = TRUE);
  19. //BOOL deleteField(CDaoTableDef *pTableDef, CString strFieldName);
  20.  
  21.  
  22. #include "stdafx.h"
  23. #include "field.h"
  24.  
  25.  
  26. // check for duplicate field name in field collection
  27. // of tabledef object
  28. // IN: pTableDef--the tabledef whose fields collection we access
  29. // IN: strFieldName--the name of the field to check for existence
  30. // RETURN: TRUE if field already exists in collection, FALSE otherwise
  31. BOOL IsExistentField(CDaoTableDef *pTableDef, CString strFieldName)
  32. {
  33.     // if the tabledef is non-existent, then the answer is obvious
  34.     if (pTableDef == NULL)
  35.         return FALSE;
  36.  
  37.     // initialize status indicator
  38.     BOOL bDuplicateFieldName = TRUE;
  39.  
  40.     // see if there is a field by this name already--duplicate
  41.     // named fields are not accepted
  42.     CDaoFieldInfo fieldInfo;    // only needed for the call
  43.  
  44.     // MFC exception handler macros used
  45.     TRY
  46.     {
  47.         // this call will throw an exception if there is no
  48.         // field by the specified name--test for duplication
  49.         pTableDef->GetFieldInfo(strFieldName, fieldInfo);
  50.     }
  51.     CATCH (CDaoException, e)
  52.     {
  53.         // if this is an 'Item not found' exception, we are
  54.         // cleared to create the field -- else this is
  55.         // a duplicate field name and we got another exception
  56.         // which is irrelevant for our purposes
  57.         if (e->m_pErrorInfo->m_lErrorCode == 3265)
  58.             bDuplicateFieldName = FALSE;
  59.     }
  60.     AND_CATCH (CMemoryException, e)
  61.     {
  62.         // do nothing--no need to process errors since this is
  63.         // just a duplicate checker
  64.     }
  65.     END_CATCH
  66.  
  67.     return bDuplicateFieldName;
  68. }
  69.  
  70. // wraps the CreateField DAO call in an exception handler
  71. // IN: pTableDef--the tabledef whose fields collection we access
  72. // IN: pFieldInfo--information used to create field
  73. // RETURN: TRUE if creation succeeds, FALSE if it fails
  74. BOOL createNewField(CDaoTableDef *pTableDef, CDaoFieldInfo *pFieldInfo)
  75. {
  76.     // if the tabledef is non-existent, then the answer is obvious
  77.     if (pTableDef == NULL)
  78.         return FALSE;
  79.  
  80.     // check for existing field with this name just to be safe
  81.     if (IsExistentField(pTableDef, pFieldInfo->m_strName))
  82.     {
  83.         AfxMessageBox(_T("A field by that name already exists."));
  84.  
  85.         // return FALSE since can't create duplicate field
  86.         return FALSE;
  87.     }
  88.  
  89.     // initialize failure indicators
  90.     BOOL bCreateFailed = FALSE;
  91.  
  92.     // create a field with the specified properties
  93.     // it is automatically appended to field collection
  94.     // of the tabledef
  95.     TRY
  96.     {
  97.         pTableDef->CreateField(*pFieldInfo);
  98.     }
  99.     CATCH (CDaoException, e)
  100.     {
  101.         // construct a meaningful message
  102.         CString message = _T("Couldn't create field--Exception: ");
  103.         message += e->m_pErrorInfo->m_strDescription;
  104.  
  105.         // display message
  106.         AfxMessageBox(message);
  107.  
  108.         // indicate failure
  109.         bCreateFailed = TRUE;
  110.     }
  111.     AND_CATCH (CMemoryException, e)
  112.     {
  113.         AfxMessageBox(_T("Failed to create field--Memory exception thrown."));
  114.  
  115.         // indicate failure
  116.         bCreateFailed = TRUE;
  117.     }
  118.     END_CATCH
  119.  
  120.     // return TRUE if creation succeeds
  121.     return (!bCreateFailed);
  122. }
  123.  
  124. // fill a fieldinfo struct with the field's properties--handle exceptions
  125. // IN: pTableDef--the tabledef whose fields collection we access
  126. // OUT: pFieldInfo--information we get from the field in the collection
  127. // IN: fieldIndex--the index into the collection for the item we want
  128. // IN: bReportErrors--if TRUE, then report any problems, else silent
  129. //     TRUE by default
  130. // RETURN: TRUE if information was obtained, FALSE indicates pFieldInfo
  131. //         does not contain informaton that was requested
  132. BOOL getFieldInfo(CDaoTableDef *pTableDef, CDaoFieldInfo *pFieldInfo,
  133.                   int fieldIndex, BOOL bReportErrors /* = TRUE */)
  134. {
  135.     // if the tabledef is non-existent, then the answer is obvious
  136.     if (pTableDef == NULL)
  137.         return FALSE;
  138.  
  139.     // initialize success indicator
  140.     BOOL bSuccess = TRUE;
  141.  
  142.     TRY
  143.     {
  144.         // try to get info on the field
  145.         pTableDef->GetFieldInfo(fieldIndex, *pFieldInfo,
  146.                                 AFX_DAO_ALL_INFO );
  147.     }
  148.     CATCH (CDaoException, e)
  149.     {
  150.         // construct a meaningful message if request
  151.         if (bReportErrors)
  152.         {
  153.             CString strMessage = _T("Couldn't get information on field--Exception: ");
  154.             strMessage += e->m_pErrorInfo->m_strDescription;
  155.  
  156.             AfxMessageBox(strMessage);
  157.         }
  158.  
  159.         // indicate failure
  160.         bSuccess = FALSE;
  161.     }
  162.     AND_CATCH (CMemoryException, e)
  163.     {
  164.         // output status if requested
  165.         if (bReportErrors)
  166.             AfxMessageBox(_T("Failed to get info on field--Memory exception thrown."));
  167.  
  168.         // indicate failure
  169.         bSuccess = FALSE;
  170.     }
  171.     END_CATCH
  172.  
  173.     // return status
  174.     return bSuccess;
  175. }
  176.  
  177. // wrap field deletion with exception handlers
  178. // IN: pTableDef--the tabledef whose fields collection we access
  179. // IN: strFieldName--name of field to delete
  180. // RETURN: TRUE if deletion succeeded, FALSE otherwise
  181. BOOL deleteField(CDaoTableDef *pTableDef, CString strFieldName)
  182. {
  183.     // if the tabledef is non-existent, then the answer is obvious
  184.     if (pTableDef == NULL)
  185.         return FALSE;
  186.  
  187.     // initialize success indicator
  188.     BOOL bSuccess = TRUE;
  189.  
  190.     // MFC exception handler macros used
  191.     TRY
  192.     {
  193.         // this call will throw an exception if there is no
  194.         // field by the specified name--test for duplication
  195.         pTableDef->DeleteField(strFieldName);
  196.     }
  197.     CATCH (CDaoException, e)
  198.     {
  199.         CString strMessage = _T("Couldn't delete the field--Exception: ");
  200.         strMessage += e->m_pErrorInfo->m_strDescription;
  201.  
  202.         AfxMessageBox(strMessage);
  203.  
  204.         // indicate failure
  205.         bSuccess = FALSE;
  206.     }
  207.     AND_CATCH (CMemoryException, e)
  208.     {
  209.         AfxMessageBox(_T("Failed to delete the field--Memory exception thrown."));
  210.  
  211.         // indicate failure
  212.         bSuccess = FALSE;
  213.     }
  214.     END_CATCH
  215.  
  216.     return bSuccess;
  217. }
  218.