[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 500
  • Last Modified:

with CDaoRecordSet is it possible to change a field definition ?

i have a table with a text field whose definition does not allow empty string;
now i want to change this field definition to allow empty strings;

the table already has records so i can't simply delete the delete the field with CDaoTableDef::DeleteField
then recreate it  (CDaoTableDef::CreateField).

I don't see how to do that neither by the Cdao classes nor by SQL
0
JP_Goblet
Asked:
JP_Goblet
  • 2
1 Solution
 
AndyAinscowCommented:
from DBDAOINT.H

#define INTERFACE _DAOField
DECLARE_INTERFACE_(_DAOField, _DAO)
      {
      STDMETHOD(get_CollatingOrder)                         (THIS_ long FAR* pl) PURE;
      STDMETHOD(get_Type)                                           (THIS_ short FAR* ps) PURE;
      STDMETHOD(put_Type)                                           (THIS_ short Type) PURE;
      STDMETHOD(get_Name)                                           (THIS_ BSTR FAR* pbstr) PURE;
      STDMETHOD(put_Name)                                           (THIS_ BSTR Name) PURE;
      STDMETHOD(get_Size)                                           (THIS_ long FAR* pl) PURE;
      STDMETHOD(put_Size)                                           (THIS_ long Size) PURE;
      STDMETHOD(get_SourceField)                               (THIS_ BSTR FAR* pbstr) PURE;
      STDMETHOD(get_SourceTable)                               (THIS_ BSTR FAR* pbstr) PURE;
      STDMETHOD(get_Value)                                     (THIS_ VARIANT FAR* pvar) PURE;
      STDMETHOD(put_Value)                                     (THIS_ VARIANT Val) PURE;
      STDMETHOD(get_Attributes)                               (THIS_ long FAR* pl) PURE;
      STDMETHOD(put_Attributes)                               (THIS_ long Attr) PURE;
      STDMETHOD(get_OrdinalPosition)                         (THIS_ short FAR* ps) PURE;
      STDMETHOD(put_OrdinalPosition)                         (THIS_ short Pos) PURE;
      STDMETHOD(get_ValidationText)                         (THIS_ BSTR FAR* pbstr) PURE;
      STDMETHOD(put_ValidationText)                         (THIS_ BSTR bstr) PURE;
      STDMETHOD(get_ValidateOnSet)                         (THIS_ VARIANT_BOOL FAR* pb) PURE;
      STDMETHOD(put_ValidateOnSet)                         (THIS_ VARIANT_BOOL Validate) PURE;
      STDMETHOD(get_ValidationRule)                         (THIS_ BSTR FAR* pbstr) PURE;
      STDMETHOD(put_ValidationRule)                         (THIS_ BSTR bstr) PURE;
      STDMETHOD(get_DefaultValue)                               (THIS_ VARIANT FAR* pvar) PURE;
      STDMETHOD(put_DefaultValue)                               (THIS_ VARIANT var) PURE;
      STDMETHOD(get_Required)                                     (THIS_ VARIANT_BOOL FAR* pb) PURE;
      STDMETHOD(put_Required)                                     (THIS_ VARIANT_BOOL fReq) PURE;
      STDMETHOD(get_AllowZeroLength)                         (THIS_ VARIANT_BOOL FAR* pb) PURE;


      STDMETHOD(put_AllowZeroLength)                         (THIS_ VARIANT_BOOL fAllow) PURE;     <-----------------------


      STDMETHOD(get_DataUpdatable)                         (THIS_ VARIANT_BOOL FAR* pb) PURE;
      STDMETHOD(get_ForeignName)                               (THIS_ BSTR FAR* pbstr) PURE;
      STDMETHOD(put_ForeignName)                               (THIS_ BSTR bstr) PURE;
      STDMETHOD(AppendChunk)                                     (THIS_ VARIANT Val) PURE;
      STDMETHOD(GetChunk)                                           (THIS_ long Offset, long Bytes, VARIANT FAR* pvar) PURE;
      STDMETHOD(_30_FieldSize)                               (THIS_ long FAR* pl) PURE;
      STDMETHOD(CreateProperty)                               (THIS_ VARIANT Name, VARIANT Type, VARIANT Value, VARIANT DDL, DAOProperty FAR* FAR* pprp) PURE;
      STDMETHOD(get_CollectionIndex)                         (THIS_ short FAR* i) PURE;
      STDMETHOD(get_OriginalValue)                         (THIS_ VARIANT FAR* pvar) PURE;
      STDMETHOD(get_VisibleValue)                               (THIS_ VARIANT FAR* pvar) PURE;
      STDMETHOD(get_FieldSize)                               (THIS_ long FAR* pl) PURE;
      };


This might work.
void ChangeFieldRequired(CDaoDatabase* pDB, LPCTSTR pszTblName, LPCTSTR pszFieldName, bool bRequired)
{
      DAOField* pField = NULL;
      DAOFields* pFields = NULL;

      CDaoTableDef tbl(pDB);
      tbl.Open(pszTblName);

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

            pField->put_AllowZeroLength((short)bRequired);    <----------------------------------------
0
 
JP_GobletAuthor Commented:
it works, thanks;
and i can also with the same method (accessing the DAO objects hidden within the CDao objects) rename a field, modify attributes of a relation, ...
0
 
AndyAinscowCommented:
It's neat, it took me a while to find this functionality some time ago.  Not everything available in DAO is exposed by the CDao classes (unfortunately).
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now