Tutorials - True DataControl > Tutorial 15 - Query Mode (part 1) |
In this tutorial you will try a powerful new feature of True DataControl: Query Mode.
Query Mode enables you to build ad-hoc query forms in a matter of minutes. True DataControl Query Mode 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 TData control and the same DataGrid are used to represent data in either mode.
Tutorial 16 will demonstrate another option that you have with True DataControl query mode: you will use a TData control and data-bound controls that are intended solely for entering queries, results will be displayed with a separate TData control in a separate DataGrid.
Start a new project.
Place the following controls on the form (Form1) as shown in the figure: a TData control (TData1), a DataGrid control (DataGrid1) and two buttons (Command1, Command2).
Set properties as follows (you can use the DataSource property page to set TData data source properties, as described in Tutorial 2):
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 Products TData1.QueryMode True DataGrid1.DataSource TData1 DataGrid1.AllowAddNew True DataGrid1.AllowDelete True Command1.Caption Query Mode Command2.Caption Apply Query |
Open the True DataControl property pages for TData1. 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 TData1, 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() TData1.QueryMode = True End Sub |
Add the following code to Command2:
Example Title |
Copy Code
|
---|---|
Private Sub Command2_Click() TData1.QueryMode = False End Sub |
The form starts in query mode: the Query button on TData1 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 TData1 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 TData Query button.
Close the program. You have successfully completed Tutorial 15.