Solved

Building a TDataSet Filter string from another string  ?

Posted on 2008-10-26
2
623 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 36

Accepted Solution

by:
Geert Gruwez earned 500 total points
Comment Utility
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 36

Expert Comment

by:Geert Gruwez
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

12 Experts available now in Live!

Get 1:1 Help Now