Solved

Building a TDataSet Filter string from another string  ?

Posted on 2008-10-26
2
624 Views
Last Modified: 2011-10-19
How do I take the filter string from the ListViewToFilter routine from this question results

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_23847836.html

 and make it a true TDataSet Filter String?


screenshot1.jpg
0
Comment
Question by:geocoins-software
  • 2
2 Comments
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 22808685
I assume you need the separate items AND ed together



procedure AddTo(var aFilter: string; aField, aOperator, aValue: string; aJoin: string = 'AND');
begin
  if aFilter = '' then 
    aFilter := Format(' %s %s %s ', [aField, aOperator, aValue])
  else 
    aFilter := aFilter + Format(' %s %s %s %s ', [aJoin, aField, aOperator, aValue]);
end;
 
procedure FilterToDataset(FStr: String; D: TDataset);
var
  SL, RL: TStringList;
  I: Integer;
  Filter: string;
begin
  Filter := '';
  SL:= TStringList.Create;
  try
    SL.Text := FStr; 
    RL:= TSTringList.Create;
    try
      RL.Delimiter := ';';
      for I:= 0 to SL.Count-1 do
      begin
        RL.DelimitedText := SL[I];
        AddTo(Filter, 
          AnsiDequotedStr(RL[0], 
          AnsiDequotedStr(RL[1]), 
          AnsiDequotedStr(RL[2]), 'AND');
      end;
    finally
      FreeAndNil(RL);
    end;
  finally
    FreeAndNil(SL);
  end; 
  D.Filtered := False;
  D.Filter := Filter;
  D.Filtered := Filter <> '';
end;

Open in new window

0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22810300
you  could ellaborate further looking within the dataset for the fieldtype
and using default ftstring if not found

procedure AddTo(var aFilter: string; aField, aOperator, aValue: string; aJoin: string = 'AND'; aDataType: TDataType = ftString);
var Temp: string;
begin
  Temp := QuotedStr(aValue);
  case aDataType of 
    ftInteger: Temp := aValue;
    ftFloat: // test for Decimal Separator
      Temp := StringReplace(StringReplace(aValue, '.', DecimalSeparator, [rfReplaceAll]), ',', DecimalSeparator, [rfReplaceAll]));
    ftDateTime: // oracle: 
      Temp := Format('%s dd/mm/yyyy hh24:mi:ss', [FormatDateTime(StringToDate(Value), 'dd/mm/yyyy hh:nn:ss')]);
    // etc
  end;
  if aFilter = '' then 
    aFilter := Format(' %s %s %s ', [aField, aOperator, aValue])
  else 
    aFilter := aFilter + Format(' %s %s %s %s ', [aJoin, aField, aOperator, aValue]);
end;
 
 
      for I:= 0 to SL.Count-1 do
      begin
        RL.DelimitedText := SL[I];
        if D.FindField(AnsiDequotedStr(RL[0]) <> nil then 
          AddTo(Filter, 
          AnsiDequotedStr(RL[0], 
          AnsiDequotedStr(RL[1]), 
          AnsiDequotedStr(RL[2]), 
          'AND', D.FieldByName(AnsiDequotedStr(RL[0])).DataType) 
        else
          AddTo(Filter, 
            AnsiDequotedStr(RL[0], 
            AnsiDequotedStr(RL[1]), 
            AnsiDequotedStr(RL[2]), 'AND');
        end;

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Dbf export problem to a Visual Foxpro application 6 187
How to debug For loops? 3 49
Twebbrowser add css to the header 3 24
Breakpoint doesn't stop in my variable 3 23
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now