IT tutorials
 
Office
 

Microsoft Access 2010 : Maintain Data Integrity (part 4) - Restricting Data by Using Validation Rules

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Product Key Free : Microsoft Office 2019 – Serial Number
4/27/2013 9:18:51 PM

4. Restricting Data by Using Validation Rules

A validation rule precisely defines the information that will be accepted in one or several fields in a record. You might use a validation rule in a field containing the date an employee was hired to prevent a date in the future from being entered. Or if you deliver orders to only certain local areas, you could use a validation rule on the postal code field to refuse entries from other areas. You can create validation rules for all data types except AutoNumber, OLE Object, and Attachment.

You can create validation rules for individual fields or for entire records:

  • Field validation. At this level, Access uses the validation rule to test an entry when you attempt to leave the field.

  • Record validation. At this level, Access uses the rule to test the contents of more than one field when you attempt to leave the record.

If a field or record doesn’t satisfy the rule, Access rejects the entry and displays a message explaining why.

You create a validation rule by building an expression. In Access jargon, the term expression is synonymous with formula. It is a combination of operators, constants, functions, and identifiers that evaluates to a single value. Access builds a formula in the format a=b+c, where a is the result and =b+c is the expression.

Tip

In addition to using expressions as validation rules, you can use them to assign properties to tables or forms, to determine values in fields or reports, to define a set of conditions that a record must meet to be included in the result of a query, and so on. 

The expression you use in a validation rule combines multiple criteria to define a set of conditions that a value in a field must meet in order to be a valid entry for that field. Multiple criteria are combined using logical, comparison, and arithmetic operators. Different types of expressions use different operators. The following are the most common operators:

  • Logical operators

    • And . This operator selects records that meet all the specified criteria.

    • Or . This operator selects records that meet at least one of the criteria.

    • Not . This operator selects records that don’t match the criteria.

  • Comparison operators

    • < . Less than

    • > . Greater than

    • = Equal to

    You can combine these basic operators to form the following:

    • <= . Less than or equal to

    • >= . Greater than or equal to

    • <> . Not equal to

    The Like operator is sometimes grouped with the comparison operators and is used to test whether or not text matches a pattern.

  • Arithmetic operators

    • + . Add

    • - . Subtract

    • * . Multiply

    • / . Divide

    A related operator, & (a text form of +) is used to concatenate (combine) two text strings.

You can type validation rules in the Validation Rule property box by hand, or you can use a tool called the Expression Builder to create them. The Expression Builder isn’t a wizard; it doesn’t lead you through the process of building an expression. It provides a hierarchical list of the most common elements that you can include in an expression and an expression box to build the expression in. You open the Expression Builder dialog box by clicking the Validation button in the Field Validation group on the Fields contextual tab, and then clicking either Field Validation Rule or Record Validation Rule. In the dialog box, you can either select functions, operators, and other elements from the list to copy them into the expression box, or you can type the expression in the expression box.

To explain a validation rule to users, you can create a message that appears if someone tries to enter an invalid value in a field. A well-crafted message tells users what data is expected in the field and what format it should be entered in. For example, the message Please enter a whole number between 1 and 99 is more useful than Invalid entry.

In this exercise, you’ll create and test a field validation rule and a record validation rule.

Set Up

You need the GardenCompany06 database you worked with in the preceding exercise to complete this exercise. Open the GardenCompany06 database, and display the FieldTest table in Datasheet view. Then follow the steps.

  1. Click the fPhone field value, and replace the current entry with 6785550101, allowing the mask to format the entry for you.

  2. Click anywhere in the fPhone field, and on the Fields contextual tab, in the Field Validation group, click the Validation button.

    image with no caption

    A list of options appears.

    Set Up

    The Validation options.

  3. Click Field Validation Rule.

    The Expression Builder dialog box opens.

    Set Up

    You build your expression in the box at the top of the dialog box.

    Let’s specify that only phone numbers in the 206 or 425 area codes can be accepted in the fPhone field.

  4. In the Expression Elements list, click Operators; in the Expression Categories list, click Comparison; and in the Expression Values list, double-click Like.

    The Like comparison operator is transferred to the expression box.

  5. In the expression box, type “206*” (including the quotation marks and the asterisk). Then type a space.

    Troubleshooting

    Be sure to include the asterisk after 206. This wildcard tells Access to allow any number of characters after the area code. It is necessary because the fPhone field contains the phone number as well as the area code.

  6. In the Expression Categories list, click Logical, and in the Expression Values list, double-click Or.

    The Or logical operator is transferred to the expression box.

  7. In the Expression Categories list, click Comparison, and in the Expression Values list, double-click Like.

    The Expression Builder inserts <<Expr>> before the Like operator as a placeholder for any other expressions you might add. You can ignore this for now.

  8. In the expression box, type “425*” (including the quotation marks and the asterisk).

  9. Double-click <<Expr>>, and press the Delete key. Then delete the extra space before Like.

    The expression now requires that the fPhone field include either of two area codes.

    Troubleshooting

    If you want to simply type an expression in the expression box, you can click Less to hide the hierarchical boxes.

  10. Click OK to close the Expression Builder dialog box.

    Access warns that existing data violates the new validation rule, because the fPhone field contains a phone number that is not in either of the required area codes.

  11. Click Yes to close the message box and keep the rule.

  12. On the Fields contextual tab, in the Field Validation group, click the Validation button, and then click Field Validation Message.

    The Enter Validation Message dialog box opens.

    Troubleshooting

    You can enter a guiding message here.

  13. In the box, type Area code must be 206 or 425, and then click OK.

    Access will display this message if someone attempts to enter a phone number with an invalid area code.

  14. Save the table, and then switch to Design view.

    In the Field Properties area, the Validation Rule property and Validation text property are set to the new rule and message.

    Troubleshooting

    If you prefer, you can enter the rule and message directly in their property boxes.

  15. Switch back to Datasheet view, click anywhere in the fPhone entry, and then press the Home key to place the cursor at the beginning of the field.

    Tip

    To select an entire field, point to the left end of the field, and when the pointer changes to a thick cross, click the mouse button.

  16. Type 3605550109, and then press Tab.

    Access displays an alert box.

    Tip

    Access warns that the area code must be either 206 or 425.

    Tip

    You will see the Was This Information Helpful link only if you have enrolled in the Microsoft Customer Experience Improvement Program. Clicking the link displays a dialog box where you can give your opinion about the usefulness of the validation text.

  17. Click OK to close the alert box, type a new phone number with one of the allowed area codes, and press Tab.

    Now let’s create a record validation rule that compares one date with another to verify that it is later.

  18. Scroll to the right, and click the Click to Add field. Then create a Date/Time field with the Medium Date format, and name the field fDate2.

  19. On the Fields contextual tab, in the Field Validation group, click the Validation button, and then click Record Validation Rule.

    The Expression Builder dialog box opens. In the Expression Elements list, the FieldTest table is selected, and its fields are displayed in the expression Categories list.

  20. In the Expression Categories list, double-click fDate2 to insert it into the expression box. Then double-click fDate.

    The Expression Builder inserts <<Expr>> between fDate2 and fDate.

    Tip

    The field names are enclosed in square brackets to indicate that they are identifiers (that is, they identify the elements this expression will use in its evaluation).

  21. In the expression box, double-click <<Expr>> to select it. Then in the Expression Elements list, click Operators; in the Expression Categories list, click Comparison; and in the Expression Values list, double-click >.

    The greater than sign replaces <<Expr>> in the expression.

  22. Click OK to close the Expression Builder dialog box, and then click Yes to keep the new rule.

  23. Create a record validation message that says fDate2 must be later than fDate.

  24. Save the table. Then replace the entry in the fDate field with 11/22/22.

  25. Click the fDate2 field, click the Calendar button, and click today’s date. Then click in the record below.

    image with no caption

    Access displays an alert box containing the record validation text.

    Tip

    You cannot leave the record until you resolve this data error.

  26. Click OK, change the value in fDate2 to 12/22/22, and then click in the record below.

Clean Up

Close the FieldTest table. Retain the GardenCompany06 database for use in later exercises.

Simple Validation Tests

The Field Validation group on the Fields contextual tab includes two commands that provide simple validation tests for fields without requiring you to build an expression:

  • Required . By default, the Required property is set to No. Selecting the Required check box in the Field Validation group sets this property to Yes, meaning that every record must have an entry in this field; it cannot be blank. (A blank field is called a Null field.)

  • Unique . By default, the Indexed property of all fields except AutoNumber fields is set to No. This property has two Yes options:

    • Yes (Duplicates OK). This option is set if you select the Indexed check box in the Field Validation group.

    • Yes (No Duplicates). This option is set if you select the Unique check box. (It is set by default for AutoNumber fields.)

    In both Yes cases, Access creates an index of the data in the field and its location, similar to the index in a book. The index speeds up data searching, because Access can look up the location of the data in the index instead of searching the actual database.

In terms of validation, selecting the Required check box will cause Access to verify that there is an entry in the field before it will accept the record. Selecting the Unique check box will cause Access to verify that no other record has the same value in the field before it will accept the field entry.

Tip

For Text, Memo, and Hyperlink fields, the Required property can be refined by the Allow Zero Length property. When this property is set to Yes (the default), you can enter an empty string (two quotation marks with nothing between them) and the field will not be considered blank. In other words, a required field can be empty but not Null. The differentiation between Null and empty might seem silly, but it becomes important if someone uses programming code to work with the database, because some commands produce different results for Null fields than they do for empty fields.

 
Others
 
- Microsoft Access 2010 : Maintain Data Integrity (part 3) - Restricting the Format of Data
- Microsoft Access 2010 : Maintain Data Integrity (part 2) - Restricting the Amount of Data
- Microsoft Access 2010 : Maintain Data Integrity (part 1) - Restricting the Type of Data
- Microsoft Project and the Project Management Domain : Using Microsoft Project with Methodologies and Life Cycles
- Microsoft Project and the Project Management Domain : Exploring Project Management Industry Standards
- Microsoft OneNote 2010 : Formatting Text (part 2) - Inserting Symbols and Special Characters, Copying and Applying Formatting with the Format Painter
- Microsoft OneNote 2010 : Formatting Text (part 1) - Setting the Default Font, Changing Basic Text Formatting, Using the Styles Gallery
- Microsoft Visio 2010 : Managing and Styling Pages (part 3) - Controlling Page Size and Orientation, Setting Page Scales
- Microsoft Visio 2010 : Managing and Styling Pages (part 2) - Working with Background Pages, Inserting Backgrounds, Titles, and Borders
- Microsoft Visio 2010 : Managing and Styling Pages (part 1) - Inserting Pages
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us