Solved

Building a TDataSet Filter string from another string  ?

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

615 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