高手帮忙解释一下Filter的用法
TblEmp.Filter:='Name='''+Trim(DlgQueryDuty.EditName.Text)+'''';
上句是什么意思呀
Filter的格式是怎样的?

解决方案 »

  1.   

    当我们在操作数据集时,往往需要对数据进行筛眩例如:一个名为Customer的数据表,它具有CustNo、CustName、Country、Address、Phone、State、TaxRate等字段,如果只想查看国别为China或顾客号大于1000的顾客记录,就需要对数据集进行过滤。经总结,有下面这些过滤方法:
        一、利用TTable和TQuery的Filter属性
        1.在设计时设置Filter属性
        例如,设置Filter为:Country=′China′然后改变Filtered属性为True(注意,Filtered为True时过滤才有效)。则只能看到对应的Country字段内容为‘China’的记录。    设置Filter时可以使用的操作符有:<、>、<=、>=、=、<>、AND、OR、NOT。
        例如,设置Filter为:CustNo>=1000andCustNo<=5000,则只能看到顾客号在1000与5000之间的顾客记录。
        2.在程序运行期间进行动态过滤
        要在程序运行时改变Filter属性,这包括两种情况:
        (1)操作符右边为常量,例如:Table1Filter:=′State′+′=′+′′′HI′′′;
       注意:字符串常量必须用三对单引号括起来。
        (2)操作符右边不为常量,可能是通过一个变量指定的值,或由一输入框给出的值。这时需要用到Format函数。其代码形式为:Table1Filter:=Format(′State′+′=′+′′′%S′′′,[StateValue]);其中StateValue为已经赋值的一个字符串变量,也可以为其他形式,例如:Edit1Text。    二、用ApplyRange筛选数据集的记录
        执行下面这段代码,将只能看到顾客号在1000至5000之间的顾客记录。组成该例程的几个过程为:         
        ApplyRange,SetRangeStart,SetRangeEnd。
        Table1SetRangeStart;
        Table1[′CustNo′]:=1000;
        Table1SetRangeEnd;
        Table1[′CustNo′]:=5000;
        Table1ApplyRange;
        注意:该过程只适用于索引的字段。如果想基于非索引字段筛选,则不妨使用一点小花招:建立假索引。实现的方法为:Table1.IndexFieldNames:=字段名;Delphi还提供了简单的调用SetRangeStart、SetRangeEnd和ApplyRange的方法,例如:Table1.SetRange([Edit1.Text],[Edit2.Text]);    三、用OnFilterRecord事件筛选OnFilterRecord事件允许按非键控字段建立筛选程序,例如:
      procedure TForm1.Table1FilterRecord(DataSet:TDataSet;varAccept:Boolean);
      begin
      Accept:=DataSet[′State′]=′CA′;
      end;
        四、用TQuery控件的SQL语句
        1.SQL语句中不包含变量和参数
      Select*fromCustomer
      WhereCustNo>=1000and CustNo<=5000
        2.SQL语句中包含参数
      Select*fromCustomer
      WhereCustNo>=:CustNo
      在运行期间给参数CustNo赋值。
         3.SQL语句中包含变量
          这时向TQuery控件添加SQL语句的代码应当这样写:   Query1.Close;
       Query1.SQL.Clear;
       Query1.SQL.Add(Format(′Select*fromCustomer′+′′+′whereState=′+′′′%S′′′,[StateValue]));
       Query1.Open;在上面的四种方法中,第四种功能最强大,使用最灵活。
      

  2.   

    就是一种TABLE带的过滤功能。
    得到符合条件的结果集。
      

  3.   

    回答的很全面,所以我不多说了。楼上的,very good!!!!
      

  4.   

    Specifies the text of the current filter for a dataset.property Filter: string;DescriptionUse Filter to specify a dataset filter. When filtering is applied to a dataset, only those records that meet a filter抯 conditions are available. Filter describes the filter condition. For example, the following filter condition displays only those records where the State field is 'CA' or 'MA':State = 'CA' or State = 'MA'When a filter is set, Blank records do not appear unless explicitly included in the filter.  For example:State <> 'CA' or State = NULLWhen a field name contains spaces, you must enclose the field name in brackets. For example:[Home State] = 'CA' or [Home State] = 'MA'Filter expressions on remote SQL tables and on client datasets support field comparisons. For example:Field1 > Field2Field comparisons are not supported against local tables (Paradox, dBASE, Access, FoxPro).For a complete list of operators you can use in filter strings, see SettingTheFilterProperty.Note: Filters are not supported on unidirectional datasets. If you set the Filter property of a unidirectional dataset, it raises an exception.The FilterOptions property controls case sensitivity and filtering on partial comparisons.Tip: Applications can set Filter at runtime to change the dataset抯 filtering condition (for example, in response to user input).
      

  5.   

    我有一个问题请教:我在BDEQUERY中使用其FILTER属性进行条件筛选时一切正常,但相同的条件拿到ADOQUERY中使用就出现错误,具体情况如下:1、BDEQUERY和ADOQUERY指向同一个数据库,其中的SQL语句相同;
    2、筛选条件如果是以下类型的情况都正确:
       (字段1='AAA')
       (字段1='AAA') OR (字段1='BBB')
       (字段1='AAA') AND (字段2='TTT')
    3、但当出现此类情况时就会出错:
       ((字段1='AAA') OR (字段1='BBB')) AND (字段2='TTT')
    4、错误信息很奇怪:
       “变量或者类型不正确,或者不在可以接受的范围之内,要不就是与其他数据冲突。”
      

  6.   

    换成
    TblEmp.Filter:='Name='+QuotedStr(Trim(DlgQueryDuty.EditName.Text));
    即可
    再看看‘QuotedStr’帮助,你马上就懂了。
      

  7.   

    ShowMessage(TblEmp.Filter)
    看一看你就明白了