DataM中的表Employee,
想对其的dept(部门)进行过滤
但下列的代码却出错,
怎么改?procedure TDataM.EmployeeBeforeOpen(DataSet: TDataSet);
begin
    if pub.LoginDcode<>'' then
    begin
        Employee.Filtered := false;
        Employee.Filter := 'left(dept,'+IntToStr(length(LoginDcode))+')='''+pub.LoginDcode+'';
        Employee.Filtered := true;
    end
end;

解决方案 »

  1.   

    'left(dept,'+IntToStr(length(LoginDcode))+')='''+pub.LoginDcode+'';这行代码的问题。DataSet的Filter不支持那么复杂的SQL代码,你可以用关键字filter搜索帮助文档,然后查看Setting the Filter property。To create a filter using the Filter property, set the value of the property to a string that contains the filter's test condition. For example, the following statement creates a filter that tests a dataset's State field to see if it contains a value for the state of California:Dataset1.Filter := 'State = ' + QuotedStr('CA');You can also supply a value for Filter based on text supplied by the user. For example, the following statement assigns the text in from edit box to Filter:Dataset1.Filter := Edit1.Text;You can, of course, create a string based on both hard-coded text and user-supplied data:Dataset1.Filter := 'State = ' + QuotedStr(Edit1.Text);Blank field values do not appear unless they are explicitly included in the filter:Dataset1.Filter := 'State <> ''CA'' or State = BLANK';NoteAfter you specify a value for Filter, to apply the filter to the dataset, set the Filtered property to True.Filters can compare field values to literals and to constants using the following comparison and logical operators: 
    Operator Meaning
    < Less than
    > Greater than
    >= Greater than or equal to
    <= Less than or equal to
    = Equal to
    <> Not equal to
    AND Tests two statements are both True
    NOT Tests that the following statement is not True
    OR Tests that at least one of two statements is True
    + Adds numbers, concatenates strings, adds numbers to date/time values (only available for some drivers)
    - Subtracts numbers, subtracts dates, or subtracts a number from a date (only available for some drivers)
    * Multiplies two numbers (only available for some drivers)
    / Divides two numbers (only available for some drivers)
    * wildcard for partial comparisons (FilterOptions must include foPartialCompare)
    By using combinations of these operators, you can create fairly sophisticated filters. For example, the following statement checks to make sure that two test conditions are met before accepting a record for display:(Custno > 1400) AND (Custno < 1500);NoteWhen filtering is on, user edits to a record may mean that the record no longer meets a filter's test conditions. The next time the record is retrieved from the dataset, it may therefore "disappear." If that happens, the next record that passes the filter condition becomes the current record.
      

  2.   

    考虑其他方法实现吧你的  dept+IntToStr(length(LoginDcode)),是否表示了一列?
      

  3.   

    回楼上
    其实就是:left(dept,2)=01我试了不用left可以还有别的办法吗?
      

  4.   

    left是SQL语句中的函数吧用这个
    function LeftStr(const AText: string; const ACount: Integer): string; 
         unit StrUtils.pas
          功能 返回字符串AText左边的ACount个字符
          说明 LeftStr('123456', 3) = '123'
          参考 function System.Copy
          例子 Edit3.Text := LeftStr(Edit1.Text, SpinEdit1.Value);Employee.Filter := 'left(dept,'+IntToStr(length(LoginDcode))+')='''+pub.LoginDcode+'';
    改为
    Employee.Filter := format('%s=%s', [LeftStr(dept,length(LoginDcode),QuotedStr(pub.LoginDcode)]); 
      

  5.   


    这种 语句不能用Filter ,你还是用sql语句吧
      

  6.   

    filter不能使用SQL,只能使用运算结果为一个逻辑值的表达式
      

  7.   

    楼主可以在OnFileRecord事件里处理!