ComponentOne True DataControl 8.0
Tutorial 11 - Imposing Constraints 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 True DataControl field, and how to cancel pending edits using the CancelUpdate method.

  1. Start a new project.

  2. Place the following controls on the form (Form1) as shown in the figure: a TData control (TData1), four TextBox controls (Text1 to 4), four labels (Label1 to 4) and a button (Command1).


  3. Set properties as follows (you can use the DataSource property page to set TData data source properties, as described in Tutorial 1):

    Example Title
    Copy Code
    TData1.ConnectionString
    
    Provider=Microsoft.Jet.OLEDB.3.51;Persist Security
    
    Info=False;Data Source=C:\Program Files\ComponentOne Studio\Common\TDDEMO.MDB
    
    TData1.CommandType   2 – adCmdTable
    
    TData1.RecordSource  Employees
    
    Text1.DataSource     TData1
    
    Text1.DataField      FirstName
    
    Text2.DataSource     TData1
    
    Text2.DataField      LastName
    
    Text3.DataSource     TData1
    
    Text3.DataField      BirthDate
    
    Text4.DataSource     TData1
    
    Text4.DataField      HireDate
    
    Command1.CausesValidation  False
    

    Set label and button captions as shown in the picture.

  4. Open the True DataControl property pages for TData1. 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 BirthDate 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()
    
        TData1.CancelUpdate
    
    End Sub
    
     
    

Run the Program and Observe the Following:

Close the program. You have successfully completed Tutorial 11.

 

 


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

Product Support Forum  |  Documentation Feedback