Field Object, Fields Collection Example (VC++)

This example creates a TableDef object and a Field object, appends the Field to the Fields collection in the new TableDef, and appends the TableDef to the TableDefs collection in the current database. The example enumerates all the fields in the new TableDef object and all the properties of the new Field. See the methods and properties listed in the Field summary topic for additional examples.

CdbDBEngine   dbeng;
CdbDatabase   dbsNorthwind;
CdbTableDef   tdfTest;
CdbField   fldTest;
long      i;

dbsNorthwind = dbeng.OpenDatabase(_T("Northwind.mdb"));
tdfTest = dbsNorthwind.CreateTableDef(_T("MyTable"));
fldTest = tdfTest.CreateField(_T("MyField"), dbDate);
tdfTest.Fields.Append(fldTest);
dbsNorthwind.TableDefs.Append(tdfTest);

// Get database name. 
printf(_T("Database Name: %s\n"), dbsNorthwind.GetName());

// Enumerate all fields in tdfTest.
printf(_T("TableDefs: Name; Fields: Name\n"));

for (i = 0; tdfTest.Fields.GetCount(); i++)
   {
   printf(_T("  %s; %s\n"), tdfTest.GetName(), tdfTest.Fields[i].GetName());
   }
   printf(_T("\n"));
// Enumerate built-in properties of fldTest.
printf(_T("fldTest.Name: %s\n"), fldTest.GetName());
printf(_T("AllowZeroLength: %d\n"), fldTest.GetAllowZeroLength());
printf(_T("Attributes: %ld\n"), fldTest.GetAttributes());
printf(_T("CollatingOrder: %ld\n"), fldTest.GetCollatingOrder());
printf(_T("DefaultValue: %s\n"), fldTest.GetDefaultValue());
printf(_T("OrdinalPosition: %d\n"), fldTest.GetOrdinalPosition());
printf(_T("Required: %d\n"), fldTest.GetRequired());
printf(_T("Size: %ld\n"), fldTest.GetSize());
printf(_T("SourceField: %s\n"), fldTest.GetSourceField());
printf(_T("SourceTable: %s\n"), fldTest.GetSourceTable());
printf(_T("Type: %d\n"), fldTest.GetType());
printf(_T("ValidationRule: %s\n"), fldTest.GetValidationRule());
printf(_T("ValidationText: %s\n"), fldTest.GetValidationText());