Tutorials - True DataControl Lite > Tutorial 7 - Imposing Constraints in TDataLite and Related Issues |
This tutorial explains how to specify constraint expressions for individual fields as well as for the whole record. It also describes how to specify default values for fields in a new record, how to use the Required and ReadOnly properties of a TDataLite field.
Start a new project.
Place the following controls on the form (Form1) as shown in the figure: an ADODC control (Adodc1), a TDataLite control (TDataLite1), five TextBox controls (Text1 to 5), and five labels (Label1 to 5) and a command button (Command1).
Set properties as follows:
Example Title |
Copy Code
|
---|---|
Adodc1.ConnectionString Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source= C:\Program Files\ComponentOne Studio\Common\TDBGDemo.mdb Adodc1.CommandType 2 – adCmdTable Adodc1.RecordSource Employees TDataLite1.DataSource Adodc1 Text1.DataSource TDataLite1 Text1.DataField FirstName Text2.DataSource Adodc1 Text2.DataField LastName Text3.DataSource Adodc1 Text3.DataField BirthDate Text4.DataSource Adodc1 Text4.DataField HireDate Text5.DataSource Adodc1 Text5.DataField Title |
Set label captions as shown in the picture.
Note: You can set the DataSource property of the five TextBox controls either to Adodc1 or to TDataLite1, it does not matter in this case. Also, we deliberately used only controls other than TDBGrid in this tutorial, to demonstrate that these features work for any data bound control. |
Open the property pages for TDataLite1. Go to the Fields page, and select the HireDate field. Type Date in the Default Expression text box to specify a default value for the HireDate field when new records are added.
Click the right mouse button over the HireDate field and select New Constraint from the context menu (alternatively, you can use the New button). An empty constraint expression for the HireDate field appears indented beneath the field name in the list, and the field controls on the right side of the page are replaced with expression editing controls. Go to the Constraint Expression text box and enter the following expression:
Example Title |
Copy Code
|
---|---|
HireDate <= Date |
(You can ignore the Condition text box for now. This is used for entering an optional condition specifying whether to apply the constraint expression or to skip it.)
Type the following text into the Message Expression text box:
Example Title |
Copy Code
|
---|---|
"Hire date can't be in the future"
|
This completes the constraint definition for the HireDate field.
Go to the Constraints property page. Create a new constraint by clicking the right mouse button over the constraints area (left part of the page) and choosing New from the context menu (alternatively, you can use the New button). The expression editing controls on the right side of the page become available. Type the following text into the Constraint Expression text box:
Example Title |
Copy Code
|
---|---|
IsEmpty(HireDate) or HireDate >= BirthDate |
Type the following text into the Message Expression text box:
Example Title |
Copy Code
|
---|---|
"You can't hire a person who is not born"
|
This completes the constraint definition for the entire record.
Return to the Fields property page. Select the Title field. Check the Required check box, which is unchecked by default.
Select the LastName field. Check the ReadOnly check box, also unchecked by default. Repeat this step for the FirstName field.
Go to the General property page and check the AddNew check box in the Buttons group. We will need the AddNew button to add records.
Add the following code to Command1:
Example Title |
Copy Code
|
---|---|
Private Sub Command1_Click() Adodc1.Recordset.CancelUpdate Adodc1.Recordset.Bookmark = Adodc1.Recordset.Bookmark End Sub |
In the first record (employee Nancy Davolio) enter 1/1/2010 in the HireDate field (its original value is 6/4/92). Press Tab, or click on another control with the mouse. The error message: "Hire date can’t be in the future" will appear in accordance with the constraint imposed on the HireDate field in Step 5.
Enter 12/8/40 in the HireDate field. Press Tab or click another control with the mouse. Although the entered date is incorrect (less than BirthDate), you are allowed to leave the control. This is because the corresponding constraint is a record-level constraint; it is checked only when you leave the record or post the change to the database. Now, try to move to another record with the TDataLite1’s Next button (or the Adodc1’s Next button). The error message: "You can't hire a person who is not born" will be displayed in accordance with the record-level constraint imposed in Step 6.
Click the UndoRecord button. This executes the ADO CancelUpdate method, which restores all fields to their original value. The strange looking second line in the code (setting Adodc1.Recordset.Bookmark to itself) is necessary because Visual Basic controls, such as TextBox, won’t recognize CancelUpdate without it. The original HireDate reappears: 6/4/92.
Clear the Title value from Text5. Then press Tab or click another field in the same row with the mouse. The error message: "Field Title can’t be left empty" will appear as specified by the Required property in Step 7.
Try to change LastName and press Tab. You will see that the field value has not changed, in accordance with the ReadOnly property you specified for these fields in Step 8.
Press the Add button on the TDataLite1 control to add a new row. The HireDate field in the newly created row will be automatically set to today’s date as returned by the VBScript function Date, which was specified in the Default Expression for the HireDate field in Step 4.
Close the program. You have successfully completed Tutorial 7.