Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Building a TDataSet Filter string from another string  ?

Posted on 2008-10-26
2
Medium Priority
?
629 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 38

Accepted Solution

by:
Geert Gruwez earned 2000 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 38

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

722 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