Solved

Building a TDataSet Filter string from another string  ?

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi IDE crash without error message ... 7 120
control image tags in a string ? 12 153
Tidtcpserver listening on multiports? 1 45
DBCtrlGrid, Delphi, Scroll 7 33
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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