Solved

Building a TDataSet Filter string from another string  ?

Posted on 2008-10-26
2
627 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

734 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