Class Fields (DAO VBA)
A Fields collection contains all stored Field objects of an Index, QueryDef, Recordset, Relation, or TableDef object. To use a Fields class variable it first needs to be instantiated, for example
Dim flds as Fields
Set flds = Workspaces(1).Databases(1).QueryDefs(1).Fields
Append
Adds a new Field to the Fields collection.
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.
Append (Object)
Object: An object variable that represents the field being appended to the collection.
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
Count
Returns the number of objects in the specified collection.
Because members of a collection begin with 0, you should always code loops starting with the 0 member and ending with the value of the Count property minus 1. If you want to loop through the members of a collection without checking the Count property, you can use a For Each...Next command. The Count property setting is never Null. If its value is 0, there are no objects in the collection.
Dim flds As DAO.Fields: Set flds =
flds.Count
Delete
Deletes a Field from the Fields collection.
The deletion of a stored object occurs immediately, but you should use the Refresh method on any other collections that may be affected by changes to the database structure.
Delete (Name)
Name: The field to delete.
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
Item
Item (Item)
Refresh
Updates the objects in the specified colletion to reflect the database's current schema.
To determine the position that the Microsoft Access database engine uses for Field objects in the Fields collection of a QueryDef, Recordset, or TableDef object, use the OrdinalPosition property of each Field object. Changing the OrdinalPosition property of a Field object may not change the order of the Field objects in the collection until you use the Refresh method Use the Refresh method in multiuser environments in which other users may change the database. You may also need to use it on any collections that are indirectly affected by changes to the database. For example, if you change a Users collection, you may need to refresh a Groups collection before using the Groups collection. A collection is filled with objects the first time it's referred to and won't automatically reflect subsequent changes other users make. If it's likely that another user has changed a collection, use the Refresh method on the collection immediately before carrying out any task in your application that assumes the presence or absence of a particular object in the collection. This will ensure that the collection is as up-to-date as possible. On the other hand, using Refresh can unnecessarily slow performance.
Sub RefreshX()
Dim dbsNorthwind As Database
Dim tdfEmployees As TableDef
Dim aintPosition() As Integer
Dim astrFieldName() As String
Dim intTemp As Integer
Dim fldLoop As Field
Set dbsNorthwind = OpenDatabase("Northwind.mdb")
Set tdfEmployees = dbsNorthwind.TableDefs("Categories")
With tdfEmployees
' Display original OrdinalPosition data and store it
' in an array.
Debug.Print _
"Original OrdinalPosition data in TableDef."
ReDim aintPosition(0 To .Fields.Count - 1) As Integer
ReDim astrFieldName(0 To .Fields.Count - 1) As String
For intTemp = 0 To .Fields.Count - 1
aintPosition(intTemp) = _
.Fields(intTemp).OrdinalPosition
astrFieldName(intTemp) = .Fields(intTemp).Name
Debug.Print , aintPosition(intTemp), _
astrFieldName(intTemp)
Next intTemp
' Change OrdinalPosition data.
For Each fldLoop In .Fields
fldLoop.OrdinalPosition = _
100 - fldLoop.OrdinalPosition
Next fldLoop
Set fldLoop = Nothing
' Print new data.
Debug.Print "New OrdinalPosition data before Refresh."
For Each fldLoop In .Fields
Debug.Print , fldLoop.OrdinalPosition, fldLoop.Name
Next fldLoop
.Fields.Refresh
' Print new data, showing how the field order has been
' changed.
Debug.Print "New OrdinalPosition data after Refresh."
For Each fldLoop In .Fields
Debug.Print , fldLoop.OrdinalPosition, fldLoop.Name
Next fldLoop
' Restore original OrdinalPosition data.
For intTemp = 0 To .Fields.Count - 1
.Fields(astrFieldName(intTemp)).OrdinalPosition = _
aintPosition(intTemp)
Next intTemp
End With
dbsNorthwind.Close
End Sub