ValidationRule, ValidationText Properties Example (MDB)
The following example sets the ValidationRule and ValidationText properties for two fields in an Order Details table. Once these properties have been set, you can view them in table Design view as well.
Sub SetValidation()
Dim dbs As Database, tdf As TableDef
Dim fldQuantity As Field, fldDiscount As Field
' Return reference to current database.
Set dbs = CurrentDb
' Return referent to Order Details table.
Set tdf = dbs.TableDefs![Order Details]
Set fldQuantity = tdf.Fields!Quantity
Set fldDiscount = tdf.Fields!Discount
' Set ValidationRule and ValidationText.
fldQuantity.ValidationRule = ">= 4"
fldQuantity.ValidationText = "Quantity must be " _
& "four or more items."
fldDiscount.ValidationRule = "Between .05 and .30"
fldDiscount.ValidationText = "Discount must be " _
& "between 5% and 30%."
Set dbs = Nothing
End Sub