DAO Object
 
Creates a new Field object (Microsoft Access workspaces only).
 

Syntax

expression .CreateField(NameTypeSize)
expression A variable that represents a TableDef object.
 

Parameters

Name
Required/optional
Data type
Description
Name
Optional
Variant
A String that uniquely names the new Field object. See the Name property for details on valid Field names.
Type
Optional
Variant
A constant that determines the data type of the new Field object. See the Type property for valid data types.
Size
Optional
Variant
An Integer that indicates the maximum size, in bytes, of a Field object that contains text. See the Size property for valid size values. This argument is ignored for numeric and fixed-width fields.
 

Return value

Field
 

Remarks

You can use the CreateField method to create a new field, as well as specify the name, data type, and size of the field. If you omit one or more of the optional parts when you use CreateField, you can use an appropriate assignment statement to set or reset the corresponding property before you append the new object to a collection. After you append the new object, you can alter some but not all of its property settings. See the individual property topics for more details.
 
The type and Size arguments apply only to Field objects in a TableDef object. These arguments are ignored when a Field object is associated with an Index or Relation object.
 
If Name refers to an object that is already a member of the collection, a run-time error occurs when you use the Append method.
 
To remove a Field object from a Fields collection, use the Delete method on the collection. You can't delete a Field object from a TableDef object's Fields collection after you create an index that references the field.
 
Link provided by the UtterAccess community. UtterAccess is the premier Microsoft Access wiki and help forum.
 

Example

The following example shows how to create a calculated field. The CreateField method creates a field named FullName. The Expression property is then set to the expression that calculates the value of the field.
 
Sub CreateCalculatedField()
        Dim dbs As DAO.Database
        Dim tdf As DAO.TableDef
        Dim fld As DAO.Field2
       
        ' get the database
        Set dbs = CurrentDb()
       
        ' create the table
        Set tdf = dbs.CreateTableDef("tblContactsCalcField")
       
        ' create the fields: first name, last name
        tdf.Fields.Append tdf.CreateField("FirstName", dbText, 20)
        tdf.Fields.Append tdf.CreateField("LastName", dbText, 20)
       
        ' create the calculated field: full name
        Set fld = tdf.CreateField("FullName", dbText, 50)
        fld.Expression = "[FirstName] & "" "" & [LastName]"
        tdf.Fields.Append fld
       
        ' append the table and cleanup
        dbs.TableDefs.Append tdf
       
    Cleanup:
        Set fld = Nothing
        Set tdf = Nothing
        Set dbs = Nothing
End Sub