Using Validation Rule property
Validation rules, sometimes referred to as data integrity rules, form a critical part of the data management strategy in a database management system (DBMS). These rules essentially determine what kind of data is permissible for entry into the database and under what conditions. By applying validation rules at the table level, you ensure that data entering the database conforms to the specified conditions, thereby maintaining data quality and integrity.
To implement validation rules, you must first understand the nature and characteristics of the data that should be stored within each table of your database. Each rule is custom-made, tailored to the specific requirements of each column in a table. There are several types of validation rules you can apply to a database, depending on your DBMS:
- Data Type Validation: This is the most basic type of validation, ensuring that data entering a field aligns with its defined data type. For instance, you cannot enter text data into an integer field.
- Range Validation: With this rule, you can specify a specific range of values allowed in a field. For example, a field storing age values might restrict input to the range 0-120.
- List Validation: This rule restricts entries to a predefined list of acceptable values. This type of validation is commonly used for fields with categorical data.
- Pattern Validation: This rule uses regular expressions to enforce a specific pattern to the data. It's typically used for fields like phone numbers or email addresses.
- Referential Integrity: This is a crucial validation rule in a relational database, which ensures the relationships between tables are maintained. For example, a foreign key in one table must match a primary key in another table.
To set up validation rules at the table level, you will use SQL (Structured Query Language) constraints during the creation or alteration of your tables. Here is an example of how you might set a validation rule at the table level in SQL:
CREATE TABLE Employees (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
Age int CHECK (Age>=18 AND Age<=65),
Email varchar(255) CHECK (Email LIKE '_%@_%._%'),
PRIMARY KEY (ID)
In this example, the CHECK constraint is used to set validation rules for the Age and Email fields. The age field must be between 18 and 65, and the email field must match the pattern of a typical email address. If data inserted into the table violates these rules, the DBMS will reject it.
It's crucial to remember that while validation rules are a powerful tool for data quality control, they are not foolproof. Therefore, they should be used in conjunction with other data quality management strategies, such as routine data audits and appropriate user access controls, to ensure comprehensive protection against bad data insertion.
Keep in mind that if you assign the Validation Rule property to a field at the table level and then assign a different
Validation Rule property to a control at the form level, Access tries to use both properties to validate the data in that field.
An example of this would be if, for a field at the table level, you said that age had to be greater than 21, but for a field at the form level, you said that age had to be less than 21. You would not be able to enter any ages, because no entry could pass both validation rules.
The following example creates a validation rule for a field that allows only values over 65 to be entered. If a number less than 65 is entered, a message is displayed. The properties are set by using the SetFieldValidation function.
Dim strTblName As String, strFldName As String
Dim strValidRule As String
Dim strValidText As String, intX As Integer
strTblName = "Customers"
strFldName = "Age"
strValidRule = ">= 65"
strValidText = "Enter a number greater than or equal to 65."
intX = SetFieldValidation(strTblName, strFldName, _
Function SetFieldValidation(strTblName As String, _
strFldName As String, strValidRule As String, _
strValidText As String) As Integer
Dim dbs As Database, tdf As TableDef, fld As Field
Set dbs = CurrentDb
Set tdf = dbs.TableDefs(strTblName)
Set fld = tdf.Fields(strFldName)
fld.ValidationRule = strValidRule
fld.ValidationText = strValidText