TableDef Object, TableDefs Collection Example (VC++)

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

CdbDBEngine      dbeng;
CdbDatabase      db;
CdbTableDef      tdfEnum;
CdbField      fldDate;
int         nFld, nIdx;

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

// Create a tabledef.
tdfEnum = db.CreateTableDef("MyTable");
fldDate = tdfEnum.CreateField("Date", dbDate);
tdfEnum.Fields.Append(fldDate);
db.TableDefs.Append(tdfEnum);

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

// Enumerate all fields in tdfEnum.
printf("TableDef: Name; Field: Name\n");
for (nFld = 0; nFld < tdfEnum.Fields.GetCount(); nFld++)
   {
   printf("  %s", tdfEnum.GetName());
   printf("; %s\n", tdfEnum.Fields[nFld].GetName());
   }

// Enumerate all indexes in tdfEnum.
printf("TableDef: Name; Index: Name\n");
for (nIdx = 0; nIdx < tdfEnum.Indexes.GetCount(); nIdx++)
   {
   printf("  %s", tdfEnum.GetName());
   printf("; %s\n", tdfEnum.Indexes[nIdx].GetName());
   }
   
// Enumerate built-in properties of tdfEnum.
printf("tdfEnum.Name: %s\n", tdfEnum.GetName());
printf("tdfEnum.Attributes: %ld\n", tdfEnum.GetAttributes());
printf("tdfEnum.Connect: %s\n", tdfEnum.GetConnect());
printf("tdfEnum.DateCreated: %s\n", tdfEnum.GetDateCreated().Format());
printf("tdfEnum.LastUpdated: %s\n", tdfEnum.GetLastUpdated().Format());
printf("tdfEnum.RecordCount: %ld\n", tdfEnum.GetRecordCount());
printf("tdfEnum.SourceTableName: %s\n", tdfEnum.GetSourceTableName());
printf("tdfEnum.Updatable: %d\n", tdfEnum.GetUpdatable());
printf("tdfEnum.ValidationRule: %s\n", tdfEnum.GetValidationRule());
printf("tdfEnum.ValidationText: %s\n", tdfEnum.GetValidationText());