Tutorials - True DataControl Lite > Tutorial 8 - Query Mode in TDataLite (part 1) |
In this tutorial you will try a powerful feature of TDataLite: Query Mode.
Query Mode enables you to build ad-hoc query forms in a matter of minutes. It is an enhanced version of the Microsoft Access “Filter By Form,” and implements the paradigm known as “Query By Example."
In this tutorial, we will create a form that can function in two modes: query and results. The same TDataLite control and the same DataGrid are used to represent data in either mode.
Tutorial 9 will demonstrate another option that you have with TDataLite query mode: you will use a TDataLite control and data-bound controls that are intended solely for entering queries, results will be displayed, in a separate DataGrid without TDataLite.
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) and two buttons (Command1, Command2).
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 Products TDataLite1.DataSource Adodc1 TDataLite1.QueryMode True DataGrid1.DataSource TDataLite1 DataGrid1.AllowAddNew True DataGrid1.AllowDelete True Command1.Caption Query Mode Command2.Caption Apply Query |
Open the TDataLite1 property pages. On the General page check the Query Mode check box in the Buttons group, which will make the Query button visible.
On the Fields property page of TDataLite1, select the ProductName field. Select starts with in the Query Operation combo box.
Add the following code to Command1:
Example Title |
Copy Code
|
---|---|
Private Sub Command1_Click() TDataLite1.QueryMode = True End Sub |
Add the following code to Command2:
Example Title |
Copy Code
|
---|---|
Private Sub Command2_Click() TDataLite 1.QueryMode = False End Sub |
The form starts in query mode: the Query button on TDataLite1 is in the selected state and the grid has a single empty row, ready for you to enter a query.
Enter 2 in the SupplierID column in the first grid row. Then enter 4 in the CategoryID column of the second grid row, and move to the first row to allow the grid to add a new row (the second row you just entered data to).
Press the Query button on TDataLite1 to leave query mode and apply query. You will see the grid fills with data, the result of the query you entered: SupplierID = 2 OR CategoryID = 4.
Press the Query button again to return to query mode. Note that the query data (two rows) is restored in the grid. Type L in the ProductName column of the first grid row.
Press the Query button to see the results. You will see less rows in the grid than in the previous query, because the new query is more restrictive: (ProductName starts with “L” AND SupplierID = 2) OR CategoryID = 4.
Experiment some more with different queries. Query conditions are connected with AND when they belong to the same row in query mode. Different rows in query mode are connected with OR.
The two command buttons setting the QueryMode property to True/False can be used instead of the standard TDataLite Query button.
Close the program. You have successfully completed Tutorial 8.