Subject Re: [IBO] Re: add an item to ib_lookupcombo
Author Paul Vinkenoog
Hello Marko,

> I have a populated lookupcombo on form selecting different products
> for a report. The user can select one of the through the lookupcombo.

I don't think you can do that easily with a LookupCombo, because its
"item list" is actually an IB_CustomGrid descendant, connected to the
lookup query. Maybe you could do it with a UNION in the lookup query.
But even then... if the user selects "all products", this may be an
illegal value for the reports query.

If I understand you correctly, what you want can best be achieved with
a plain, non data-aware, ComboBox.

> In the possible selection I would like to add an item to the
> lookupcombo (at the beginning) with text:("0" for column ID and "all
> products" for column PRODUCTS).
> Then I would check the if the ID=0 and then make a sql without a
> where clause.

This is what I would do:

- Design your report query *without* a where clause for the product(s)
- Design an IB_Cursor for the table containing the product names
- Drop a plain TComboBox on the form - let's call it cbProductSelect
- Make a procudure FillProductSelect that works roughly like this:
* clear cbProductSelect.Items, then add item "All products";
* prepare product cursor if necessary;
* loop through product table using APIFirst and APINext on cursor;
* in every iteration, add the product name to cbProductSelect.Items;
This procedure has to be called after connection, and/or whenever the
contents of the Products table may have changed (I often use events
to signal this)
- Make an OnChange handler for cbProductSelect that does this:
* ReportQuery.InvalidateSQL;
* ReportQuery.Refresh;
- Make an OnPrepareSQL handler for the report query like this:
if cbProductSelect.ItemIndex > 0 then ReportQuery.SQLWhereItems.Add
( 'PRODUCTNAME=' + AnsiQuotedStr( cbProductSelect.Text, '''' ) );

Together, cbProductSelect.OnChange and ReportQuery.OnPrepareSQL ensure
that ReportQuery's SQL will be rebuilt every time the user selects
another item, *and* that a "where productname = ..." clause will be
appended only if the user doesn't select the top item.

Now if you want the user to see product *names* in the ComboBox but
you need a product *ID* for the where item, it gets a little more
complicated, but it can still be done.


An alternative approach is the use of two parameters in the query:
one to signal the choice between "all products" and "one product",
and the other to specify the product chosen. I've never done it like
this myself, but this alternative has been discussed here in the last
few months, with examples too. Maybe you can check the archives.


Greetings,
Paul Vinkenoog