Question : change size of text field via DAO

I want to change the size of a text field in an MS Access database programatically (via DAO ver. 4).  I have code for changing the field name which worked so I modified it to change the size.  Here it is. The code runs, no errors BUT the field size is still the same afterwards.
Have I misunderstood the put_Size purpose?  
Can I do it this way?

(A method which would work and which I could code would be to change the field name, create new field, copy contents, then delete original field - so please don't suggest that route)


void ChangeFieldTextSize(CDaoDatabase* pDB, LPCTSTR pszTblName, LPCTSTR pszFieldName, long lSize)
{
      CDaoTableDef tbl(pDB);
      tbl.Open(pszTblName);

      DAOField* pField = NULL;
      DAOFields* pFields = NULL;

      TRY {
            tbl.m_pDAOTableDef->get_Fields(&pFields);
            COleVariant vName(pszFieldName,VT_BSTRT);      
            pFields->get_Item(vName, &pField);

            pField->put_Size((long)lSize);
      }
      CATCH_ALL(e)
      {
            //Clean up before we re-throw the exception
            tbl.Close();
            if (pFields != NULL)
                  pFields->Release();
            if (pFields != NULL)
                  pFields->Release();

            //Re-throw the exception so that the caller can catch it or let MFC catch it.
            THROW_LAST();
      }
      END_CATCH_ALL

Answer : change size of text field via DAO

if your database is set to use the ANSI SQL-92 Extensions (In MS Access, Tools --> Options --> Tables/Queries, see the bottom right frame) you can use DDL

ALTER TABLE myTable ALTER myColumn datatype, size

as for using DAO  ... from the help file for DAO Field.Size property ...
... For an object not yet appended to the Fields collection ...

so once it is a part of the table it cannot be changed with DAO.

and I suspect the same thing for .DefinedSize proerty of a Column object in ADOX.

Steve
Random Solutions  
 
programming4us programming4us