Description
A Field object represents a column of data with a common data type and a common set of properties.
Remarks The Fields collections of Index, QueryDef, Relation, and TableDef objects contain the specifications for the fields those objects represent. The Fields collection of a Recordset object represents the Field objects in a row of data, or in a record. You use the Field objects in a Recordset object to read and set values for the fields in the current record of the Recordset object. In both Microsoft Jet and ODBCDirect workspaces, you manipulate a field using a Field object and its methods and properties. For example, you can:See Also CreateField method.
Specifics (Microsoft Access) In addition to the properties defined by the Microsoft Jet database engine, a Field object in the Fields collection of a QueryDef object or a TableDef object may also contain the following Microsoft Access application-defined properties. For details on reading and setting these properties, see the individual properties and the Property object.
|
|
|
|
|
|
|
|
Sub FieldX()
Dim dbsNorthwind As Database
Dim rstEmployees As Recordset
Dim fldTableDef As Field
Dim fldQueryDef As Field
Dim fldRecordset As Field
Dim fldRelation As Field
Dim fldIndex As Field
Dim prpLoop As Property
Set dbsNorthwind = OpenDatabase("Northwind.mdb")
Set rstEmployees = _
dbsNorthwind.OpenRecordset("Employees")
' Assign a Field object from different Fields
' collections to object variables.
Set fldTableDef = _
dbsNorthwind.TableDefs(0).Fields(0)
Set fldQueryDef =dbsNorthwind.QueryDefs(0).Fields(0)
Set fldRecordset = rstEmployees.Fields(0)
Set fldRelation =dbsNorthwind.Relations(0).Fields(0)
Set fldIndex = _
dbsNorthwind.TableDefs(0).Indexes(0).Fields(0)
' Print report.
FieldOutput "TableDef", fldTableDef
FieldOutput "QueryDef", fldQueryDef
FieldOutput "Recordset", fldRecordset
FieldOutput "Relation", fldRelation
FieldOutput "Index", fldIndex
rstEmployees.Close
dbsNorthwind.Close
End Sub
Sub FieldOutput(strTemp As String, fldTemp As Field)
' Report function for FieldX.
Dim prpLoop As Property
Debug.Print "Valid Field properties in " & strTemp
' Enumerate Properties collection of passed Field
' object.
For Each prpLoop In fldTemp.Properties
' Some properties are invalid in certain
' contexts (the Value property in the Fields
' collection of a TableDef for example). Any
' attempt to use an invalid property will
' trigger an error.
On Error Resume Next
Debug.Print " " & prpLoop.Name & " = " & _
prpLoop.Value
On Error GoTo 0
Next prpLoop
End Sub
Example (Microsoft Access)
The following example creates a new Field object, sets some of its properties, and appends it to the Fields collection of a TableDef object. The procedure then enumerates all fields in the Fields collection of the TableDef object.
Sub NewField()
Dim dbs As Database, tdf As TableDef
Dim fld As Field
' Return reference to current database.
Set dbs = CurrentDb
' Return reference to Employees table.
Set tdf = dbs.TableDefs!Employees
' Create new Field object.
Set fld = tdf.CreateField("SSN#")
' Set Type and Size properties of Field object.
fld.Type = dbText
fld.Size = 11
' Append field.
tdf.Fields.Append fld
' Enumerate all fields in Fields collection of TableDef object.
For Each fld in tdf.Fields
Debug.Print fld.Name
Next fld
Set dbs = Nothing
End Sub