Applies To Field object, Index object.
Description
Sets or returns a value that indicates whether a Field object requires a non-Null value or whether all the fields in an Index object must have a value.
Settings and Return Values The setting or return value is a Boolean that is True if a field can't contain a Null value. For an object not yet appended to a collection, this property is read/write. For an Index object, this property setting is read-only for objects appended to Indexes collections in Recordset and TableDef objects. Remarks The availability of the Required property depends on the object that contains the Fields collection, as shown in the following table.If the Fields collection belongs to a | Then Required is |
Index object | Not supported |
QueryDef object | Read-only |
Recordset object | Read-only |
Relation object | Not supported |
TableDef object | Read/write |
See Also AllowZeroLength property, ValidateOnSet property, ValidationRule property, ValidationText property, Value property.
Example This example uses the Required property to report which fields in three different tables must contain data in order for a new record to be added. The RequiredOutput procedure is required for this procedure to run.Sub RequiredX()
Dim dbsNorthwind As Database
Dim tdfloop As TableDef
Set dbsNorthwind = OpenDatabase("Northwind.mdb")
With dbsNorthwind
' Show which fields are required in the Fields
' collections of three different TableDef objects.
RequiredOutput .TableDefs("Categories")
RequiredOutput .TableDefs("Customers")
RequiredOutput .TableDefs("Employees")
.Close
End With
End Sub
Sub RequiredOutput(tdfTemp As TableDef)
Dim fldLoop As Field
' Enumerate Fields collection of the specified TableDef
' and show the Required property.
Debug.Print "Fields in " & tdfTemp.Name & ":"
For Each fldLoop In tdfTemp.Fields
Debug.Print , fldLoop.Name & ", Required = " & _
fldLoop.Required
Next fldLoop
End Sub
Example (Microsoft Access)
The following example creates a new Index object on an Employees table and sets the Required property for the index. The new index consists of two fields, LastName and FirstName.
Sub NewIndex()
Dim dbs As Database, tdf As TableDef, idx As Index
Dim fldLastName As Field, fldFirstName As Field
' Return reference to current database.
Set dbs = CurrentDb
' Return reference to Employees table.
Set tdf = dbs.TableDefs!Employees
' Create new index.
Set idx = tdf.CreateIndex("FullName")
' Create and append index fields.
Set fldLastName = idx.CreateField("LastName", dbText)
Set fldFirstName = idx.CreateField("FirstName", dbText)
idx.Fields.Append fldLastName
idx.Fields.Append fldFirstName
' Ensure value is entered for each field in index.
idx.Required = True
' Append Index object.
tdf.Indexes.Append idx
Set dbs = Nothing
End Sub