ComponentOne True DataControl 8.0
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.

  1. Start a new project.

  2. 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).

  3. 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.
  4. 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.

  5. 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.

  6. 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.

  7. Return to the Fields property page. Select the Title field. Check the Required check box, which is unchecked by default.

  8. Select the LastName field. Check the ReadOnly check box, also unchecked by default. Repeat this step for the FirstName field.

  9. 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.

  10. 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
    
     
    

Run the Program and Observe the Following:

Close the program. You have successfully completed Tutorial 7.

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback