CreateField Method Example

This example uses the CreateField method to create three Fields for a new TableDef. It then displays the properties of those Field objects that are automatically set by the CreateField method. (Properties whose values are empty at the time of Field creation are not shown.)

Sub CreateFieldX()

   Dim dbsNorthwind As Database
   Dim tdfNew As TableDef
   Dim fldLoop As Field
   Dim prpLoop As Property

   Set dbsNorthwind = OpenDatabase("Northwind.mdb")

   Set tdfNew = dbsNorthwind.CreateTableDef("NewTableDef")

   ' Create and append new Field objects for the new 
   ' TableDef object.
   With tdfNew
      ' The CreateField method will set a default Size
      ' for a new Field object if one is not specified.
      .Fields.Append .CreateField("TextField", dbText)
      .Fields.Append .CreateField("IntegerField", dbInteger)
      .Fields.Append .CreateField("DateField", dbDate)
   End With

   dbsNorthwind.TableDefs.Append tdfNew

   Debug.Print "Properties of new Fields in " & tdfNew.Name

   ' Enumerate Fields collection to show the properties of 
   ' the new Field objects.
   For Each fldLoop In tdfNew.Fields
      Debug.Print "  " & fldLoop.Name

      For Each prpLoop In fldLoop.Properties
         ' Properties that are invalid in the context of
         ' TableDefs will trigger an error if an attempt
         ' is made to read their values.
         On Error Resume Next
         Debug.Print "    " & prpLoop.Name & " - " & _
            IIf(prpLoop = "", "[empty]", prpLoop)
         On Error GoTo 0
      Next prpLoop

   Next fldLoop

   ' Delete new TableDef because this is a demonstration.
   dbsNorthwind.TableDefs.Delete tdfNew.Name
   dbsNorthwind.Close

End Sub