TEstSearchDialogs

Searchdialogs arose from an idea by Eric ten Westenend to allow adding search capabilities to a database application in an easy way. Not reprogramming a search functionality over and over again, not several different implementations of searching throughout the application, no per-application solution anymore. A standard way of searching that is easy to use for the end-user and easy to implement for the programmer.

While Eric was able to develop his idea to quite a professional level, I still saw possibilities to extend the idea. I also wanted to allow other Delphi developers to use the powerful solution freely. Erik Stok search dialogs is what resulted.

This document contains the following:

Download
Installation
Using searchdialogs
The components
Examples
Version history

Download

The latest version of ErikStok search dialogs can be downloaded from the ErikStok searchdialog download on ErikStok.net.

Installation

Unzip the file to a location of choice. Open the package EstSearchDialog.dpk and compile it. Then open the package EstSearchDialogDesignTime.dpk and install it.

After that, open the runtime package for your data access layer of choice (for example for ADO this is EstSearchDialogAdo.dpk) and compile it. Then open the designtime package for that data access layer (for ADO this is EstSearchDialogAdoDesignTime.dpk) and install it.

On the component pallet a new tab Erik Stok will appear containing the search dialog components of all data access layers you have compiled and installed.

Using searchdialogs

The searchdialog is a non-visual component that can be triggered from anywhere in the application by calling the Execute method to allow a user to search through data. The execute method returns by its result wether the user has found something or not. By using the ResultFieldAs... methods information on the record that was found can be acquired.

The search is based on a query defined by the programmer. This kan be a simple query on a single table, but also several tables can be joined to allow the user to select from combined data. The programmer decides on what fields of the query the user can search.

A big problem with allowing users to search in this manner is database performance. A user could start a query that has a result of many records. To prevent that, the possibility to limit the result set to a maximum number of records has been added. If a maximum has been set, first a count query is performed to check the number of matching records. If the maximum is exceeded, the user is asked to supply more specific search specifications.

Also it is not always wanted that the user can combine a big number of search criteria. Therefore the facility to limit the number of search criteria a user can select is also implemented.

Searching on dates if often relative to the moment in time. For that reason special date values have been added. A user can search on an explicit date by entering the date in the local date format, but also the words day, week, month, quarter and year can be used. If this is done, the start of the current day, week, month, quarter or year is used (weeks are ISO based, for quarters jan-mar is Q1, apr-jun is Q2, etc). Also special times are available in a similar way by using the words minute and hour. With special dates and times, the user can also enter an offset. So yesterday is "day-1", tomorrow is "day+1", last week is "week-1", etc.

The components

All available components descend from the same ancestor: TEstSearchDialog. Implementations are made for the data access layers BDE, DB Express, ADO, Interbase Express, Zeos, NexusDB en FlashFiler.

Properties

The components have the following properties:

Designtime

AdditionalWhere

If the final searchquery constructed by the base query and the users search criteria should be extended with an aditional where clause, this property can be used to set it. This can be used when before starting the searchdialog information is available on what extra critaria should be filtered, for example an orders search dialog that should only search in orders of the customer that the user currently sees. This prevents dynamically having to change the SearchQuery property.

CompareFormatDate

Databases use different comparison syntaxes for date field. One database compares a date field to YYYYMMDD (without quotes), another expects 'M/D/YYYY' or even #DDMMYYYY#. Set this property to the correct syntax for the database you are using. The CompareFormatDate string is a formatstring compatible with Delphi's FormatDateTime format strings. If the database expects the value to be quoted, use the QuotedDateTime property. Also mind that a / means: the date separator according to your regional settings. A litteral / can be achieved by using '/' (see also the Delphi help on FormatDateTime).

CompareFormatTime

As CompareFormatDate, but for time comparisons.

CompareFormatDateTime

As CompareFormatDate, but for datetime comparisons..

Connection

This is the database connection the dialog uses to gain access to the database. For different access layers this is a reference to different connection components (TSQLConnection, TAdoConnection, TIBDatabase etc).

CountQuery

This is the query that is used to count the number of result records when the Max property is set to a value other then 0. It is important to keep the CountQuery compatible with the SearchQuery when it comes to fields, tables and any aliases used, because the same WHERE clause is built as for the SearchQuery. An example of a query: SELECT count(*) FROM customer WHERE %WHERE%.

CriteriaCount

This is the number of criteria the search dialogs initially opens with. This can never exceed MaxCriteria. If the Store property indicates that the last search should be stored, CriteriaCount is overruled by that.

DecimalChar

Decimal sign used for float fields in the SQL query.

DialogCaption

Caption of the dialog.

DialogHeight

Dialog height. If set to 0, the dialog has the default height.

DialogWidth

Dialog width. If set to 0, the dialog has the default width.

Max

This indicates the maximum number of result records the search result may contain. If more records are found than this maximum, the user is requested to supply more specific search criteria. If Max is set to any other value than 0, the CountQuery property must also be set. If Max is set to 0, the number of result records is unlimited (default setting).

MaxCriteria

This indicates the maximum number of criteria the user is allowed to specify.

QuoteChar

The character used the surround string values when searching on them. Some databases expect string values to be quoted with a " character (instead of a ' character) in the WHERE clause,this property allows you to configure this.

QuotedDateTime

If the database compares date values with surrounding quotes, set this property to true.

RegistryPath

If the Store property indicates the last search should be stored, the RegistryPath property indicates what location under HKEY_CURRENT_USER this is stored. If this property is left empty, the default location, HKEY_CURRENT_USER\Software\ErikStok\<FormClass>\<ComponentName> is used.

SearchDialogFieldList

This is a collection of fields the search dialog uses. Per field several properties can be set. The FieldName of a field determines what query field the field settings are connected to. The WhereSyntax states the syntax that would be used by the database when the field is used in a WERE clause including all aliases. The DisplayLabel property is the column caption of the field in the result grid and DisplayWidth determines the width of the column. FieldType indicates the type of the field and thus on what values can be searched. Search indicates if the user can search on this field or that the field is only displayed in the result.

SearchQuery

This is the query used to query the actual result. To this query a number of conditions are added based on the criteria set by the user. This is done by replacing a where tag, %WHERE%, in the query by an actual where clause. An example of a query: SELECT custno, company FROM customer WHERE %WHERE% ORDER BY company.

SearchStyle

It is possible to start a non-modal search by changing the default value ssModal of this property to ssNormal or ssMDIChild. If SearchStyle is set to ssNormal, the dialog is displayed as a non-modal form. With ssMDIChild the form can even be made a child form (under the condition that there is an MDIForm available). If ssNormal or ssMDIChild is used, the Execute method always returns False immediately (no result has been selected). Using the OnSelect event it is possible to handle a select from the dialog. Using the OnClose event a close of the dialog can be handled.

StartOpen

By setting this property the dialog starts with the result of the query with the WHERE clause set to TrueExpression and AdditionalWhere (if available). Be careful with this property, it can cause a lot of records to be retrieved and it can also conflict with the Max property value.

Store

Store can be used to indicate if and how the last search should be stored. The values that can be used are dsNone (last search is not stored), dsFields (only the selected fields of the last search are stored) and dsFieldsAndValues (both fields and values of the last search are stored). The last search is stored in the registry on the location indicated by the RegistryPath property. Only when a record is selected a store takes place.

TrueExpression

If no criteria are detected to perform the search, this is used to fill the %WHERE% tag to still create a valid query. This only happens in designtime when running the query or in runtime when StartOpen is set. Most databases accept 1=1 as a true expression, so this often does not need to be changed.

Runtime

ResultQuery

After executing a search for which a user has selected a record, this property contains the SQL statement that led to the complete result set the user selected from.

Methods

Execute

Execute shows the search dialog. If the user closes the dialog with OK (a record is selected) this method returns True. If the user closes the dialog with Cancel, this method returns False. If SearchStyle is set to display the dialog as non-modal, this method always returns False and OnSelect and OnClose should be used to detect a record selection or dialog close.

ResultFieldAsString

Field values of the result record selected by the user can be read by using the ResultFieldAs... methods. This particluar method is used to read back a field of the result record as a string.

ResultFieldAsInteger

As with ResultFieldAsString, but this method is used to read back a field value as Integer.

ResultFieldAsFloat

As with ResultFieldAsString, but this method is used to read back a field value as Float.

ResultFieldAsDateTime

As with ResultFieldAsString, but this method is used to read back a field value as DateTime.

FullRegistryPath

If the registrypath that the Store functionality uses during runtime must be read, this method can be used.

Events

OnAfterExecuteCountQuery

This event occurs just after the count query has been executed.

OnAfterExecuteSearchQuery

This event occurs just after the search query has been executed.

OnBeforeExecuteCountQuery

This event occurs just before the count query will be executed.

OnBeforeExecuteSearchQuery

This event occurs just before the search query will be executed.

OnClose

This event occurs when the dialog is closed. Using this event it is possible to determine that the user has stopped selecting records for dialogs with a SearchStyle other than ssModal.

OnFilterRecord

This event works just like the OnFilterRecord event on a dataset, but in this case the dataset is the search result dataset.

OnSelect

This event occurs when the user has selected a record in a dialog with a SearchStyle other than ssModal.

Examples

In the folder EstSearchDialogDemo of the download a demo application is available that demonstrates all possibilites of the search dialogs. This demo application makes use of the DBDemos Access database via ADO, so this database must be available and the ADO implementation of EstSearchDialog must be installed to view and run this demo.

Version history

02-01-2004: The components are currently in Alpha stage.
04-01-2004: Switched to different BDE connection link. Alias is now entered as a string.
11-01-2004: Made several fixes, AdditionalWhere, events and special date values added.
12-01-2004: Several small bugfixes and added DecimalChar property.
18-01-2004: Now also a non-modal search can be done by using the SearchStyle property. Demo added.
24-01-2004: Also english documentation is available.