Microsoft KB Archive/152688

= How to change the Name property of a Microsoft Access field in Visual C++ =

Article ID: 152688

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.1 Subscription

 Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q152688

<div class="summary_section">

SUMMARY
The MFC DAO classes do not provide a programmatic way to change the name of a Microsoft Access field. The example code in this article changes a field's Name property by directly calling DAO using the m_pDAOFields data member of CDaoTableDef.

<div class="moreinformation_section">

Sample Code
///////////////////////////////////////////////////////////////////   // CTabDef - class definition

class CTabDef : public CDaoTableDef {  public: CTabDef( CDaoDatabase* pDB) : CDaoTableDef( pDB ) { }

~CTabDef {}

void ChangeFieldProperty(LPCTSTR szOldName, LPCTSTR szNewName); };

/////////////////////////////////////////////////////////////////   // CTabDef - implementation void CTabDef::ChangeFieldProperty( LPCTSTR szOld, LPCTSTR szNew ) {    // To be used as an "in" param to get_Item DAOField* pDAOField = NULL;

if (m_pDAOFields == NULL) InitFieldsCollection;

// Enumerate the fields collection and find the field. // Then replace the name property with the new string. try {      DAO_CHECK( m_pDAOFields->get_Item( COleVariant(szOld, VT_BSTRT), &pDAOField ) );

DAO_CHECK( pDAOField->put_Name( COleVariant(szNew, VT_BSTRT).bstrVal ) ); }      catch( CDaoException* e ) {          AfxMessageBox( e->m_pErrorInfo->m_strDescription ); goto term; }      catch(...) {          AfxMessageBox( _T( "unknown exception" ) ); goto term; }

term: if( pDAOField ) pDAOField->Release; }

Keywords: kbhowto kbinfo kbdatabase kbprogramming kbcode KB152688

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.