AutoSearch data pipeline fields can have their default values configured through Calc tab code. To do this, first make sure the field is setup as AutoSearch = True on the Data tab, then goto Calc tab, select the top Report object in the outline tree and create an event on ReportBeforeAutoSearchDialogCreate. Below is example code, followed by an explanation:


var
ppTemp : TppAutoSearchField;
begin
ppTemp := Report.AutoSearchCriteriaByName('EVENTS', 'COMPANY');
if ppTemp <> nil then
begin
ppTemp.SearchExpression := 'ABC COMPANY';
end;

if ReplaceSpecialFields('!PROFILE!') = 'System Administrator' then
begin
ppTemp := Report.AutoSearchCriteriaByName('EVENTS', 'DEPARTMENT');
if ppTemp <> nil then
begin
ppTemp.SearchOperator := soInList;
ppTemp.SearchExpression := 'DEPT1,DEPT2,DEPT3';
end;
end;

ppTemp := Report.AutoSearchCriteriaByName('EVENTS', 'EVENT_DATE');
if ppTemp <> nil then
begin
ppTemp.SearchExpression := DateToStr(CurrentDate - 7) + ',' + DateToStr(CurrentDate);
end;
end;

The above code assumes that there are 3 fields setup as AutoSearch fields on the Data tab for the EVENTS table pipeline: COMPANY, DEPARTMENT, and EVENT_DATE. This code sets new default values for all 3 AutoSearch fields. The first section of code looks for an existing auto-search field in the 'EVENTS' pipeline named 'COMPANY'. The value is then setup to 'ABC COMPANY'. Granted, in this case the 'ABC COMPANY' value could just as easily have been set as a default directly on the Data tab. But often it is helpful to hardcode a 'default' value for AutoSearch fields on the Data tab that insures the user that any initial calls to active this pipeline that take place BEFORE the auto-search dialog will not return many (or any) records. So the Calc tab is a good place to make sure the default is set back to an appropriate value.

In the 2nd section of code, the 'DEPARTMENT' field is modified from its default filter value, but only when the report is run by a 'System Administrator'. Let's say the default filter value for 'DEPARTMENT' as setup on the Data tab is '= DEPT3'. This is typically a good default, but if a 'System Administrator' is running the report, they usually need to report on multiple departments at once, and the default should be to include all 3. So note that first the SearchOperator is changed to soInList (from the default of soEqual [=] setup on the Data tab). It is rare to have to change the SearchOperator in Calc tab code, but if required, these are the acceptable values:


soEqual = The field value must be the same as the search value.
soNotEqual = The field value must not be the same as the search value.
soLessThan = The field value must be less than the search value.
soLessThanOrEqualTo = The field value must be less than or equal to the search value.
soGreaterThan = The field value must be greater than the search value.
soGreaterThanOrEqualTo = The field value must be greater than or equal to the search value.
soLike = The field value must begin with the search value. A wildcard is automatically placed at the end of the search criteria value which use this operator, making it function as a 'begins with'
soNotLike = The field value must not begins with the search value.
soBetween = The field value must fall between the two search criteria values.
soNotBetween = The field value must not fall between the two search criteria values.
soInList = The field value must appear in the list of search values.
soNotInList = The field value must not appear in the list of search values.
soBlank = The field value must be null. (No search value is required for this operator.)
soNotBlank = The field value must not be null. (No search value is required for this operator.)



Next, in this second example the SearchExpression is set to 'DEPT1,DEPT2,DEPT3'. Since the SearchOperator is now 'soInList', the SearchExpression responds to a comma-delimited listing of values.

Finally, we dynamically set the 2 dates for a date range. When setting up the default filter Value on the Data tab, it is uncommon/unlikely for users to know the best default dates - since typically these dates are relative to the date when the report is run. This example assumes that the EVENT_DATE field is setup as soBetween ['Between'] on the Data tab. So in this case the SearchExpression is also going to require a comma-delimited listing of the 2 dates - the 'from' and the 'to'. Use the DateToStr function to convert dates to string (text) values. Review the Code Toolbox window (lower-right)...Language section to be reminded of other useful functions like CurrentDate.

Below is a second example that defaults an EVENT_DATE field to all events in the current month:

var
ppTemp : TppAutoSearchField;
iYear, iMonth, iDay, iToYear, iToMonth : Integer;
begin
ppTemp := Report.AutoSearchCriteriaByName('EVENTEQUIP', 'EVENT_DATE');
if ppTemp <> nil then
begin
DecodeDate(CurrentDate, iYear, iMonth, iDay);
if iMonth = 12 then
begin
iToMonth := 1;
iToYear := iYear + 1;
end else
begin
iToMonth := iMonth + 1;
iToYear := iYear;
end;
ppTemp.SearchExpression := DateToStr(EncodeDate(iYear, iMonth, 1)) + ',' +
DateToStr(EncodeDate(iToYear, iToMonth, 1));
end;
end;

It is also possible to create completely new AutoSearch criteria in Calc tab code (rather than simply modifying existing AutoSearch field values). Below is an example that either adds the new criteria or modifying an existing criteria, as appropriate:

ppTemp := Report.AutoSearchCriteriaByName('EVENTS', 'COMPANY');
if ppTemp = nil then
Report.CreateAutoSearchCriteria('EVENTS', 'COMPANY', soEqual, 'ABC COMPANY', False) else
ppTemp.SearchExpression := 'ABC COMPANY';

The Report.CreateAutoSearchCriteria procedure takes 5 parameters:


  1. name of the pipeline
  2. name of the field (omit any table name qualifications like 'GAGES.' or 'EVENTS.', etc.)
  3. the comparison type - see above list for potential values
  4. default value for AutoSearch filter, ok to have blank values for text fields
  5. whether the AutoSearch filter is Mandatory - enter either True or False


One final note: if you are able to use the Calc tab code to completely set the values for all AutoSearch fields and you are sure that end-users will not need to modify them in the AutoSearch dialog (or should not be allowed to modify them), then it is possible to disable this AutoSearch dialog right before it is called. Simply add the line of code below somewhere in the same ReportBeforeAutoSearchDialogCreate event discussed above:

Report.ShowAutoSearchDialog := False;



Keywords: AutoSearch , N/A , N/A

Rev: 1.1 - 10/23/2009 - BAS - added numbered bullets

Rev: 1.0 - 10/23/2009 - BAS - Article Created