Lazarus

Programming => Databases => Topic started by: cpalx on November 03, 2018, 09:50:21 pm

Title: BufDataset | filter with and/or
Post by: cpalx on November 03, 2018, 09:50:21 pm
hello,

how could i do something like this?

Code: Pascal  [Select][+][-]
  1. with bufdataset1 do begin
  2.    filtered:= false;
  3.    filter:= format(  'field1= ''%s''  or field2= ''%s:0'' ', [edit1.text] );
  4.    filtered:=true;
  5. end;
Title: Re: BufDataset | filter with and/or
Post by: lucamar on November 03, 2018, 09:56:44 pm
I don't know the answer but about the Format ... shouldn't it be like this:

Format( 'field1= ''%s''  or field2= ''%s:0'' ', [edit1.text] );

???
Title: Re: BufDataset | filter with and/or
Post by: cpalx on November 03, 2018, 10:02:05 pm
@lucamar you are right about the format. I will edit my post.

the problem goes on
Title: Re: BufDataset | filter with and/or
Post by: wp on November 03, 2018, 10:05:28 pm
There are two format parameters in the format string, but there is only one string. Either you apply a second parameter
Code: Pascal  [Select][+][-]
  1. Format('field1= ''%s'' or field2=''%s''', [Edit1.Text, Edit2.Text]);
or if there is only one parameter needed twice you can use an index in the format string as proposed by lucamar; but use the correct syntax:
Code: Pascal  [Select][+][-]
  1. Format('field1= ''%0:s'' or field2=''%0:s''', [Edit1.Text]);
See: https://www.freepascal.org/docs-html/rtl/sysutils/format.html
Title: Re: BufDataset | filter with and/or
Post by: lucamar on November 03, 2018, 10:36:08 pm
[...] but use the correct syntax:
Code: Pascal  [Select][+][-]
  1. Format('field1= ''%0:s'' or field2=''%0:s''', [Edit1.Text]);

Oops! Sorry, I did, indeed, invert the expression. I never remember all the nuances of Format :-[

Re. the expression itself, I have been looking into the sources and found that the filter is parsed in unit dbf_prscore. Its header apparently points to some info:
Code: Pascal  [Select][+][-]
  1. unit dbf_prscore;
  2.  
  3. {--------------------------------------------------------------
  4. | TCustomExpressionParser
  5. |
  6. | - contains core expression parser
  7. |
  8. | This code is based on code from:
  9. |
  10. | Original author: Egbert van Nes
  11. | With contributions of: John Bultena and Ralf Junker
  12. | Homepage: http://www.slm.wau.nl/wkao/parseexpr.html
  13. |
  14. | see also: http://www.datalog.ro/delphi/parser.html
  15. |   (Renate Schaaf (schaaf at math.usu.edu), 1993
  16. |    Alin Flaider (aflaidar at datalog.ro), 1996
  17. |    Version 9-10: Stefan Hoffmeister, 1996-1997)
  18. |
  19. |---------------------------------------------------------------}
  20.  
  21. interface
  22.   [. . .]
  23.  
TinyPortal © 2005-2018