Size and Type Properties Example (VC++)

This example creates a new Field object with a Text field type, sets its Name property to Fax Phone and its Size property to 20, and appends the new object to the Fields collection of the Employees table in the TableDefs collection of the database.

CdbDBEngine      dbeng;
CdbDatabase      dbsNorthwind;
CdbTableDef      tdfEmployees;
CdbField         fldFaxPhone;

// open database
dbsNorthwind = dbeng.OpenDatabase(_T("Northwind.mdb"));
// get existing table reference
tdfEmployees = dbsNorthwind.TableDefs.Item(_T("Employees"));
// create field object
fldFaxPhone = tdfEmployees.CreateField(_T("Fax Phone"));
// set field properties
fldFaxPhone.SetType(dbText);
fldFaxPhone.SetSize(20);
// append fldFaxPhone to Fields collection
tdfEmployees.Fields.Append(fldFaxPhone);
return TRUE;

You can also use the CreateField method if you provide name, type, and size as arguments.

FldFaxPhone = tdfEmployees.CreateField(_T("Fax Phone"), dbText, 20);