Fields.Append 方法 (DAO)
适用于:Access 2013、Office 2013
语法
表达式 。追加 (对象)
表达式 表示 Fields 对象的变量。
参数
名称 |
必需/可选 |
数据类型 |
说明 |
---|---|---|---|
Object |
必需 |
Object |
一个对象变量,代表追加到集合的字段。 |
说明
使用 Append 方法可以将新表添加到数据库中,将字段添加到表中,以及将字段添加到索引中。
使用 Delete 方法删除追加的对象之前,该对象是存储在磁盘上的永久对象。
添加新对象会立即发生,但是,对于受数据库结构更改影响的其他任何集合,应使用 Refresh 方法。
如果追加的对象不完整(例如,如果在将某个 Index 对象追加到 Indexes 集合之前,没有将任何 Field 对象追加到该对象的 Fields 集合),或者一个或多个从属对象中的属性集不正确,则使用 Append 方法会导致错误。 例如,如果未指定字段类型,并且尝试将 Field 对象追加到 TableDef 对象的 Fields 集合,则使用 Append 方法将触发运行时错误。
示例
此示例使用 Append 方法或 Delete 方法来修改 TableDef 的 Fields 集合。 要使该过程运行,需要 AppendDeleteField 过程。
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