Share via


Fields.Append Method (DAO)

Adds a new Field to the Fields collection.

Syntax

expression .Append(Object)

expression A variable that represents a Fields object.

Parameters

Name

Required/Optional

Data Type

Description

Object

Required

Object

An object variable that represents the field being appended to the collection.

Remarks

You can use the Append method to add a new table to a database, add a field to a table, and add a field to an index.

The appended object becomes a persistent object, stored on disk, until you delete it by using the Delete method.

The addition of a new object occurs immediately, but you should use the Refresh method on any other collections that may be affected by changes to the database structure.

If the object you're appending isn't complete (such as when you haven't appended any Field objects to a Fields collection of an Index object before it's appended to an Indexes collection) or if the properties set in one or more subordinate objects are incorrect, using the Append method causes an error. For example, if you haven’t specified a field type and then try to append the Field object to the Fields collection in a TableDef object, using the Append method triggers a run-time error.

Example

This example uses either the Append method or the Delete method to modify the Fields collection of a TableDef. The AppendDeleteField procedure is required for this procedure to run.

Sub AppendX() 
 
 Dim dbsNorthwind As Database 
 Dim tdfEmployees As TableDef 
 Dim fldLoop As Field 
 
 Set dbsNorthwind = OpenDatabase("Northwind.mdb") 
 Set tdfEmployees = dbsNorthwind.TableDefs!Employees 
 
 ' Add three new fields. 
 AppendDeleteField tdfEmployees, "APPEND", _ 
 "E-mail", dbText, 50 
 AppendDeleteField tdfEmployees, "APPEND", _ 
 "Http", dbText, 80 
 AppendDeleteField tdfEmployees, "APPEND", _ 
 "Quota", dbInteger, 5 
 
 Debug.Print "Fields after Append" 
 Debug.Print , "Type", "Size", "Name" 
 
 ' Enumerate the Fields collection to show the new fields. 
 For Each fldLoop In tdfEmployees.Fields 
 Debug.Print , fldLoop.Type, fldLoop.Size, fldLoop.Name 
 Next fldLoop 
 
 ' Delete the newly added fields. 
 AppendDeleteField tdfEmployees, "DELETE", "E-mail" 
 AppendDeleteField tdfEmployees, "DELETE", "Http" 
 AppendDeleteField tdfEmployees, "DELETE", "Quota" 
 
 Debug.Print "Fields after Delete" 
 Debug.Print , "Type", "Size", "Name" 
 
 ' Enumerate the Fields collection to show that the new 
 ' fields have been deleted. 
 For Each fldLoop In tdfEmployees.Fields 
 Debug.Print , fldLoop.Type, fldLoop.Size, fldLoop.Name 
 Next fldLoop 
 
 dbsNorthwind.Close 
 
End Sub 
 
Sub AppendDeleteField(tdfTemp As TableDef, _ 
 strCommand As String, strName As String, _ 
 Optional varType, Optional varSize) 
 
 With tdfTemp 
 
 ' Check first to see if the TableDef object is 
 ' updatable. If it isn't, control is passed back to 
 ' the calling procedure. 
 If .Updatable = False Then 
 MsgBox "TableDef not Updatable! " & _ 
 "Unable to complete task." 
 Exit Sub 
 End If 
 
 ' Depending on the passed data, append or delete a 
 ' field to the Fields collection of the specified 
 ' TableDef object. 
 If strCommand = "APPEND" Then 
 .Fields.Append .CreateField(strName, _ 
 varType, varSize) 
 Else 
 If strCommand = "DELETE" Then .Fields.Delete _ 
 strName 
 End If 
 
 End With 
 
End Sub