14th Chapter-Simple database application creation and Mastapp Introduction (iii) (4)

Source: Internet
Author: User
Tags empty valid

14.8 Validation of input data

When a user inserts a new record into a database table or modifies an existing record, we must ensure that the data entered by the user is valid, and that Delphi is used in three different ways to verify that the data entered by the user is valid.

These three ways are based on database table validation, field based validation, and record based validation.

Validation based on database tables:

When a user creates a database table, a validation mechanism is established, such as when creating a table using DBD, we can illustrate some validation tools for the database tables that are created, including the maximum value of the field, the minimum value, the display format of the graphics field, and so on. When you set these validation mechanisms, you do not need to write any program code. Validation based on database tables is performed by the database itself before the data is written to the database. Delphi also performs some validation, such as when the data is written to the database and Delphi verifies that each field is filled with the corresponding value, please refer to the use of DBD for details on this approach to verify the validity of the data.

Field-based validation:

There are generally two ways to validate this approach.

① writes OnValidate event handling for fields in the record that need to set validation. This way, whenever the value of the field is modified, the OnValidate event handling for that field is invoked, which verifies the value of the field being modified.

② for fields that are not empty in the record, such as passwords or keywords, we must first set the Required property of these fields to true, and then write the OnValidate event handling procedure for those fields, so that when you modify an existing record or insert a new record, before you write to the database, If the required field value is not filled in for a non-empty field, an error message prompts the user to enter the field value.

Record-based validation:

This type of validation is typically handled during the Beforepost event processing of the ttable part, which verifies that each field value of the record is validated before the record is written back to the database.

Example 14.8 validates the validity of a field value in a program.

1. Create an application that browses the Orders.db table with the Tedit part, as shown in Figure 14.25.

2. The Autoedit property for modifying the Tdatasource part is true.

3. Double-click the ttable part to open the Field editor fields Editor, and the Saledate field.

4. In Object Inspector, double-click the OnValidate event of the Saledate Field object to write an event handling procedure for the Field object as follows:

Tform1.table1saledatevalidate (Sender:tfield);

Begin

If Saledate.value>now Then

Raise Exception.create (' cannot enter a future date ');

End

When the application runs, when the user modifies or inserts a record in Orders.db, the application validates the value of the sales date (saledate) field, which cannot be later than the current date of the system, and the current method is called in the program to obtain the system's present date. If the field value is greater than the current date of the system, an error message balloon informs the user that a future date cannot be entered.

Use the Tdbcombox parts and tdblookupcombox parts to limit the range of user input field values.

Create an application that views the Orders.db table, and create a good form as shown in Figure 14.25. The terms field displayed in the form is the Tdbcombox part, and the Empno field is the Tdblookupcombox part.

Figure 14.25 Restricting user input with a data browsing widget

The property values for the Tdbcombox and Tdblookupcombox parts are shown in table 14.8:

Table 14.8 property settings for each part in the form

━━━━━━━━━━━━━━━━━━━━━━━━━━━

Part Attribute Property value

───────────────────────────

DataField Terms

DBComBox1 DataSource DataSource1

Items Prepaid

Net 30

Cod

───────────────────────────

DataField EmpNo

DataSource DataSource1

Dblookupcombox Lookupsource DataSource2

Keyfield EmpNo

Lookupfield EmpNo

───────────────────────────

DataSource1 DataSet Table1

Autoedit True

───────────────────────────

DataSource2 DataSet Table1

Autoedit True

───────────────────────────

Table1 DatabaseName Demosdb

TableName orders.db

───────────────────────────

Table2 DatabaseName Demosdb

TableName orders.db

━━━━━━━━━━━━━━━━━━━━━━━━━━━

When the application runs, when the user modifies and inserts records into the Orders.db table, the value of the terms field can be selected from the prepaid, Net30, cod three values in the combo box, and the value of the Empno field is the employee number obtained from the other table employee. The user can select from.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.