>

CreateField Method

Applies To

Index Object, Relation Object, TableDef Object.

Description

Creates a new Field object.

Syntax

Set variable = object.CreateField([name[, type [, size]]])

The CreateField method syntax has these parts.

Part

Description

variable

A variable declared as an object data type Field.

object

The variable name of the Index, Relation, or TableDef object you want to use to create the new Field object.

name

A String variable that uniquely names the new Field object. See the Name property for details on valid Field names.

type

An Integer constant that determines the data type of the new Field object. See the Type property for valid data types.


Part

Description

size

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.


Remarks

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.

Note

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 Index and Relation objects.

If name refers to an object that is already a member of the collection, a trappable error occurs when you use the Append method.

To remove a Field object from a collection, use the Delete method on the collection. You can't delete a Field object from a TableDef object's Fields collection once an index has been created that references the field.

See Also

Append Method, Delete Method, Field Object, Name Property, Size Property, Type Property.

Example

This example creates a new Field object.


Dim dbsNorthwind As Database
Dim tdfEmployees As TableDef
Dim fldPhone As Field
Set dbsNorthwind = DBEngine.Workspaces(0).OpenDatabase("Northwind.mdb")
Set tdfEmployees = dbsNorthwind.TableDefs("Employees")
' Create new Field object.
Set fldPhone = tdfEmployees.CreateField("Phone", dbText)
' Set another property of fldPhone.
fldPhone.Size = 25    ' Greater than longest phone number.
' Save fldPhone definition by appending it to Fields collection.
tdfEmployees.Fields.Append fldPhone
dbsNorthwind.Close
Example (Microsoft Access)

The following example creates a new Field object and appends it to an Employees table.


Sub NewField()
    Dim dbs As Database, tdf As TableDef
    Dim fld As Field

    ' Return Database variable pointing to current database.


    Set dbs = CurrentDb
    Set tdf = dbs.TableDefs!Employees
    ' Create new Field object.
    Set fld = tdf.CreateField("Phone", dbText)
    ' Set another property of field.
    fld.Size = 25
    ' Append new field to Fields collection.
    tdf.Fields.Append fld
End Sub