Tutorials - True DataControl Lite > Tutorial 9 - Query Mode in TDataLite (part 2) |
This tutorial is the second part of the lesson begun in Tutorial 10. It demonstrates another option that you have with TDataLite Query Mode: you will create a TDataLite control and data-bound controls intended solely for entering queries. Query results will be displayed in a separate DataGrid unrelated to the TDataLite control. Also, you will learn how to specify more complex query conditions, such as ranges, etc.
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), a DataGrid control (DataGrid1), seven TextBox controls (Text1 to 7), seven labels (Label1 to 7) and a 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 Orders TDataLite1.DataSource Adodc1 TDataLite1.QueryMode True TDataLite1.EOFAction adDoAddNew Adodc2.ConnectionString Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=C:\Program Files\ComponentOne Studio\Common\TDBGDemo.mdb Adodc2.CommandType 2 – adCmdTable Adodc2.RecordSource Orders DataGrid1.DataSource Adodc2 |
Open the property pages for TDataLite1, and go to the Fields page. Delete all fields except the following: OrderID, CustomerID, ShipName (to delete a field, select it in the field list and press Del).
In the Fields property page for TDataLite1, select the ShipName field and change its QueryOperation property, select starts with in the Query Operation combo box.
In the Fields property page for TDataLite1, create four additional fields (by clicking the right mouse button and choosing New Field from the menu). For each of them select the Data radio button, indicating that the field is a data field. For each of the four new fields, set its Name, DataSourceField and QueryOperation properties, typing in the Name edit box and selecting from the lists in the DataSource Field and Query Operationcombo boxes.
Note: You must change Query Mode to 0 - Both or 1 - Query only for the Query Operation combo box to become available. |
Name: |
DataSource Field: |
Query Operation: |
OrderDateFrom |
OrderDate |
>= |
OrderDateTo |
OrderDate |
<= |
RequiredDateFrom |
RequiredDate |
>= |
RequiredDateTo |
RequiredDate |
<= |
Set properties of other controls as follows:
Text1.DataSource |
TDataLite1 |
Text1.DataField |
OrderID |
Text2.DataSource |
TDataLite1 |
Text2.DataField |
CustomerID |
Text3.DataSource |
TDataLite1 |
Text3.DataField |
OrderDateFrom |
Text4.DataSource |
TDataLite1 |
Text4.DataField |
OrderDateTo |
Text5.DataSource |
TDataLite1 |
Text5.DataField |
ShipName |
Text6.DataSource |
TDataLite1 |
Text6.DataField |
RequiredDateFrom |
Text7.DataSource |
TDataLite1 |
Text7.DataField |
RequiredDateTo |
Set label and button captions according to the picture.
Add the following code to Command1:
Example Title |
Copy Code
|
---|---|
Private Sub Command1_Click() Adodc2.Recordset.Filter = TDataLite1.QueryFilter End Sub |
We leave it to you to experiment with different queries and their results, as it was done in Tutorial 8. We just want you to notice the following:
Now you have a sophisticated query form where you can specify different criteria, including date ranges. Four new fields created on Step 5 support data range conditions. ShipName condition is "starts with" rather than "equals," that was specified on Step 4. Field values in a record generate conditions joined with AND.
You can specify complex queries with more than one condition joined with OR, simply by adding new records to the query form. To add a new record to query, simply pressing the Next button on TDataLite1 when it is positioned on the last record. This is enabled by our setting TDataLite1.EOFAction=adDoAddNew.
Pressing the Apply button applies the specified query to Adodc2. TDataLite1 remains in query mode at all times.