Topic: Logiocal functions in Grid filer

Hi,
Maybe a silly question, but i can´t get around it.

I have a field "Types.type" and in the grid i want to show only records containing "Receipt" or "Invoice" in that field..

My filter formula is Types.type = "Receipt" it shows records containing  that fieldvalue.

But i really need en logical operator so i can select both "Receipt" or "Invoice" ---> Types.Type = "Receipt" or "Invoice"

I have tryed everything and i skimmed through your forum, but i didn´t find anything.

Thank´s in advance

2 (edited by derek 2019-02-01 17:19:11)

Re: Logiocal functions in Grid filer

Hi Cujos,
Not a silly question at all.
If you set up a filter directly in the grid (as it sounds like you're doing), then it's 'fixed' so you can't select anything different when you run your program.  One solution would be to display 2 grids - one with a fixed filter showing all invoices and one with a fixed filter showing all receipts.
However, I think the better way to do it is to use the 'search' facility which allows you to filter records according to the value selected in your search criteria field.
Please have a look at the attached.  In this example, the search criteria is a combobox but depending on what you are searching for, it could just as easily be an edit field, a date or another tablegrid.
Just shout if anything's not clear.
Derek.

Post's attachments

Attachment icon cujos.zip 336.72 kb, 369 downloads since 2019-02-01 

3 (edited by mathmathou 2019-02-01 23:21:31)

Re: Logiocal functions in Grid filer

Hello Cujos and Derek,


I had a look at Derek's submission and, as always, it's spot on and without a single line of code !! Nice job


Everyone on this forum knows that I'm a big fan of complicated solutions and lines of code are like poetry to me.
The code bellow was a saturday morning coffee challenge, and might interest you if you want to know what happens in MVD but, again, Derek's solution is THE solution.

Oh, by the way old friends, I don't post often but read the forum every day smile


What the Tablergrid does not show but understands from what Derek as set up is :


SELECT DISTINCT
     "document"."record_count" as "document.record_count",
     "document"."docdate" as "document.docdate",
     "doctype"."doctype" as "doctype.doctype",
     "document".id,
     "document"."id_doctype" as "document.id_doctype"
FROM
     "document"
LEFT OUTER JOIN "doctype" ON "document"."id_doctype"="doctype".id
ORDER BY
     "document"."record_count" ASC


What does the "hidden search" button do when you select an item in the combobox ? It's filtering the SQL query that gets the complete list of document from the database (the query above).


To filter this, when you change your selection in the combobox, MVD injects a line in the above query :

When the combobox says "Invoice", the filtering line looks like :

WHERE document.id_doctype = 1

and is located just before the "ORDER BY" line. (1 is the id of the Invoice doc type in the database).


When the combobox says "Receipt", the filtering line looks like :

WHERE document.id_doctype = 2

And when the combobox is empty, there are many possibilities.
The simplest one is just to remove the "WHERE" line of course,

but this is equivalent to :

WHERE document.id_doctype = 1 OR document.id_doctype = 2 
--the filter has to be repeated, document.id_doctype = 1 OR 2 does not work

and also equivalent to

WHERE document.id_doctype IN (1,2)

And finally, if you disconnect the "hidden search" button from combobox by removing the incremental search parameter in the combobox, you could replace all this, easy and efficient, by this, heavy and unpractical code smile

procedure Form1_ComboBox1_OnCloseUp (Sender: TObject); //fires each time the combobox is closed
begin
    if Form1.ComboBox1.ItemIndex = 0 then //if first item of combobox is selected
        begin
            Form1.TableGrid1.dbFilter := '1 = 1';
            //Form1.TableGrid1.dbFilter := 'id_doctype = 1 OR id_doctype = 2';  //EQUIVALENT
            //Form1.TableGrid1.dbFilter := 'id_doctype IN (1,2)'; //EQUIVALENT
            Form1.TableGrid1.dbUpdate; //this executes the filtering
        end
    else if Form1.ComboBox1.ItemIndex = 1 then //else if second item of the combobox is selected
        begin
            Form1.TableGrid1.dbFilter := 'id_doctype = 1';
            Form1.TableGrid1.dbUpdate;
        end
    else if Form1.ComboBox1.ItemIndex = 2 then //else if third item of the combobox is selected
        begin
            Form1.TableGrid1.dbFilter := 'id_doctype = 2';
            Form1.TableGrid1.dbUpdate;
        end;
end;

I've made you a little package, just if you are curious smile


Again, as I already said, Derek's solution is perfect and simple.
Mine is complicated and won't even work anymore if you had another document type to you collection. It will have to be adapted in order to work, where Derek's will still work.

This was just for "academical interest"... well, at least it interests me smile


Cheers to all and wish you a good week-end


Math

Post's attachments

Attachment icon cujos_scripted.zip 335.37 kb, 381 downloads since 2019-02-02 

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Logiocal functions in Grid filer

HI, and thanks for the answer Derek..

If i put my problem into your example it would be like selecting two values  (Receipt and Invoice) from the ComboBox, and that isn´t possible, or?

But now i have solved it. The filter formula looks like this (Typer.typ = "Receipt") OR (Typer.Typ = "Invoice"). My beginners misstake was that only the radiobutton "Show child records (if present)" was on.

Sorry for that...

5 (edited by derek 2019-02-03 15:26:30)

Re: Logiocal functions in Grid filer

Hi,
No problem - at some stage, we've all left the 'show child records' button selected!
In answer to your question, yes, it is possible to filter on more than one value (see the option to select multiple values from the combobox in the attachment).  However, this function was introduced around Version3 so if you're using the free version of MVD, that won't be possible for you.
Please see the attachment for some examples of the different ways to filter (by no means all of the ways).  All of the ways in the attachment use standard MVD functionality (ie, no script is required - sorry Mathias!).
Derek.

Post's attachments

Attachment icon cujos filters.zip 339.78 kb, 475 downloads since 2019-02-03 

Re: Logiocal functions in Grid filer

Ok, Derek... Thank´s