Zum Hauptinhalt springen

Adding/Modifying AutoSearch fields

Flag Note: the topic below only applies to Custom reports (not Standard reports that use the 'Design' mode)

AutoSearch data pipeline fields can have their default values configured through the Calc tab code. To do this, first, make sure the field is set up as AutoSearch = True on the Data tab, then go to the Calc tab, select the top Report object in the outline tree, and create an event on ReportBeforeAutoSearchDialogCreate. Below is an 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 three fields set up 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 the code looks for an existing auto-search field in the 'EVENTS' pipeline named 'COMPANY.' The value is then set up to 'ABC COMPANY.' Granted, in this case, the 'ABC COMPANY' value could have been set as default directly on the Data tab. But it is often helpful to hardcode a 'default' value for AutoSearch fields on the Data tab that ensures the user that any initial calls to active this pipeline BEFORE the auto-search dialog will not return many (or any) records. So the Calc tab is a good place to ensure 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.' The default filter value for 'DEPARTMENT' as set up 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 the 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 uses this operator, making it function as a 'begins with.'

soNotLike = The field value must not begin 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 two 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 set up as soBetween ['Between'] on the Data tab. So, in this case, the SearchExpression will also require a comma-delimited listing of the two 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 the Calc tab code (rather than simply modifying existing AutoSearch field values). Below is an example that either adds the new criteria or modifies 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 five 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 the 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

Note: If you can use the Calc tab code to set the values for all AutoSearch fields completely 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; |