Property Object, Properties Collection Example (VC++)

This example creates a user-defined property for the current database, sets its Type and Value properties, and appends it to the Properties collection of the database. Then the example enumerates all properties in the database. See the properties listed in the Property summary topic for additional examples.

CdbDBEngine      dbeng;
CdbWorkspace    wrkDefault;
CdbDatabase   db;
CdbProperty   prpUserDefined, prpEnum;
COleVariant   var("This is a user-defined property.", VT_BSTRT),
         varBstr;
int         i;

// Open the database.
db = dbeng.OpenDatabase(_T("Northwind.mdb"));

// Create user-defined property.
prpUserDefined = db.CreateProperty();
   
// Set properties of new property.
prpUserDefined.SetName("UserDefinedProperty");
prpUserDefined.SetType(dbText);
prpUserDefined.SetValue((LPVARIANT)var);

// Append property to current database.
db.Properties.Append(prpUserDefined);

// Enumerate all properties of current database.
printf("Properties of Database %s\n", db.GetName());
for (i = 0; i < db.Properties.GetCount(); i ++)
   {
   prpEnum = db.Properties[i];
   printf(" Properties(%d)\n", i); 
   printf("  Name: %s\n",  prpEnum.GetName());
   printf("  Type: %d\n", prpEnum.GetType());
   
   // Change type of variant to BSTR for printing.
   try
      {
      varBstr.ChangeType(VT_BSTR, var);
      printf("  Value: %s\n", (LPCSTR)varBstr.pbstrVal);
      }
   catch (CdbException e)
      {
      printf("  Value: Value could not be converted to a string\n");
      }
   
   printf("  Inherited: %d\n", prpEnum.GetInherited());
   }

This example shows how you can set an application-defined property (or any user-defined property that may not yet exist) without causing a run-time error. The example sets an arbitrary property of a Field object. The return value of the function is True if the value was properly set. The return value is False if an unexpected error occurs when the property is set. See the properties listed in the Property summary topic for additional examples.

LONG SetFieldProperty(
   CdbDBEngine &dbe,
   CdbField &fldPropVal, 
   LPCTSTR pstrName,
   int nType, 
   LPVARIANT pvarValue)
   {
#define ERR_PROPERTY_NONEXISTENT 3270
   CdbProperty prpUserDefined; 
   LONG      lErr;
   
   // Set the field property value.
   try 
      {
      fldPropVal.Properties[pstrName].SetValue(pvarValue);
      }
   
   catch (CdbException exSetPropValue)
      {
      lErr = dbe.Errors[(LONG)0].GetNumber();
      if (lErr != ERR_PROPERTY_NONEXISTENT)
         {
         return lErr;
         }
      else
         {
         // Create Property object, setting Name, Type, and Value properties.
         try
            {
            prpUserDefined = fldPropVal.CreateProperty(pstrName, nType, pvarValue);
            fldPropVal.Properties.Append(prpUserDefined);
            }
         catch (CdbException exUDP)
            {
            return dbe.Errors[(LONG)0].GetNumber();
            }
         }
      }
   return NOERROR;
   }

int main()
   {
   CdbDBEngine dbeng;
   CdbDatabase db;
   CdbTableDef td;
   CdbField   fld;
   COleVariant var("This is a user-defined property.", VT_BSTRT);
   LONG lErr;
   
   // Open the database.
   db = dbeng.OpenDatabase(_T("Northwind.mdb"));
   
   // Get a reference on a table.
   td = db.TableDefs[_T("Employees")];

   // Get a reference on the 'name' field.
   fld = td.Fields[_T("Last Name")];
   
   // Set the field property.
   lErr = SetFieldProperty(dbeng, fld, _T("foo"), dbText, (LPVARIANT)var);
   
   return (lErr == NOERROR) ? 0 : -1;
   }