Use a validation rule instead of the Required property

Reader Štefan Masič provides today's Power Tip.

When you want to require users to enter a value in a field, a common technique is to set the Required property for that field to Yes (for example, tabPerson.Name.Required = Yes). However, when the user is then adding records to the table and neglects to insert value in that field, the built-in error message that appears is not very helpful:

"The field <TableName>.<FieldName> cannot contain Null value because the Required property is set to True. Enter a value in this field."

This error message is not appropriate for typical users, whether it's in English or any other language. Instead, I use another simple approach when building table properties:

  1. Set Table.Field.Required = No
  2. Set Table.Field.Validation Rule = IS NOT NULL
  3. Set Table.Field.Validation Text = The value in field <Caption of field name> is required. Enter a value or select it from the list.

In this way, you can provide more specific information and helpful hints about how to enter the correct data, rather than just a "required" message.

Note: When converting an Access table into SQL Server we must consider this design and set this table property manually.

Send your Power Tips to Mike & Chris at accpower@microsoft.com.

Office Blogs Comments

Comments: (2) Collapse

  • Excellent advice. Just to not miss out the alternative; the Access controls such as textbox, combobox, option groups and some more supports Validation Rule/Text properties as well, which can also enable us to customize the rule on per-form basis and still have it work with ODBC-linked tables as well. This also can be used in conjunction with validation rules as stored on the table, though the validation in control will occur before the field's validation.

  • Validation rules don't work correctly in Access 2007. I have tried to set ValidationRule = Is Not Null (ValidationText = Enter a value) for a text field but Access ignores the rule. The text field Index = 1 (second field in the form).

    There have been problems with validation rules since Access 97. In some occassions it was very easy to skip the validation rule by double Escape!

Comments

Comments: (loading) Collapse