Options

SETFILTER and SETRANGE on one field

randrewsrandrews Member Posts: 135
Hi.

I need dynamicaly set several filter to one field.
I.e. sumary filter (values may be another)
myvar := 'VALUE1';
myvar := '';
myvar := 'VALUE2';
myvar := 'VALUE 3 BLANK';

SETRANGE(Field,myvar) - erase previous filter.
Doing
TextFilter += (myvar+'&');
SETFILTER(Field,TextFilter)
Then dificulty on checkin values with blank ''. Checkin if first value myvar<>'' (filter '&Value1&value2' wrong). Checking last value (then TextFilter += myvar, and not TextFilter += (myvar+'&');)

I can do it, but code is very bulky for that simple task.

May be somebody have a simple decision?

Comments

  • Options
    jreynoldsjreynolds Member Posts: 175
    If you want a logical AND to combine your filters then you can use filter groups and set each filter in a different filter group.
  • Options
    kinekine Member Posts: 12,562
    I think that there is no 'simple' solution. There is only 'complex' solution which must do all checks. In which way, it is on you... :-)

    You can be inspired of code from Form 18 from function GetSelectionFilter which take all selected records and create the filter string for them. Automatically use .. or | based on selected records.

    If you modify this code, you can use it for your need.

    [Edited after posted]
    Sorry... I forget that this code is not calculating with empty values.. ](*,) :-#
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    kinekine Member Posts: 12,562
    Another solution:

    Do checking of all empty values and replace them with string '%1'
    Then do adding into one string...

    use
    SETFILTER(Field,TextFilter,'') 
    

    It means that all %1 will be correctly replaced with empty string.

    if the result is ''&VALUE2&VALUE3&'' is correct, ''&'' is equal to '' ('' is empty string) :-)

    Have you fixed count of values or it is dynamic?
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    randrewsrandrews Member Posts: 135
    First of all, i am mistaken
    TextFilter += (myvar+'&'); \\wrong
    TextFilter += (myvar+'|'); \\right

    Count of values is dinamicaly changes.
    Realy it deends on table SomeTable.
    SomeTable.SETRANGE(Fild1,...)
    SomeTable.SETRANGE(Fild2,...)
    And all values SomeTable.Field3 is values for filtering second table.
    REPEAT
    TestFilter += SomeTable.Field3 +'|';
    UNTIL SomeTable.NEXT =0;
    SecondTable.SETFILTER(SomeField,TestFilter)
  • Options
    kinekine Member Posts: 12,562
    randrews wrote:
    First of all, i am mistaken
    TextFilter += (myvar+'&'); \\wrong
    TextFilter += (myvar+'|'); \\right

    Count of values is dinamicaly changes.
    Realy it deends on table SomeTable.
    SomeTable.SETRANGE(Fild1,...)
    SomeTable.SETRANGE(Fild2,...)
    And all values SomeTable.Field3 is values for filtering second table.
    REPEAT
    TestFilter += SomeTable.Field3 +'|';
    UNTIL SomeTable.NEXT =0;
    SecondTable.SETFILTER(SomeField,TestFilter)
    & vs | - a thought about it and I wonder that it is AND and not OR... :-)

    my corrected code for you:
    TestFilter := '';
    IF SomeTable.FIND('-') then begin
      REPEAT
        if  SomeTable.Field3 = '' then
          TestFilter += '%1|'
        else
          TestFilter += SomeTable.Field3 +'|'; 
      UNTIL SomeTable.NEXT =0
      TestFilter := DELSTR(TestFilter,StrLen(TestFilter));  //for deleting last |
    end else
      TestFilter := 'somevalue if nothing to filter';
    
    SecondTable.SETFILTER(SomeField,TestFilter,'');
    

    another version
    TestFilter := '';
    IF SomeTable.FIND('-') then begin
      REPEAT
        if  SomeTable.Field3 = '' then
          if TestFilter = '' then
            TestFilter := '%1'; 
          else
            TestFilter += '|%1'
        else
          if TestFilter = '' then
            TestFilter := SomeTable.Field3
          else
            TestFilter += '|' + SomeTable.Field3 ; 
      UNTIL SomeTable.NEXT =0
    end else
      TestFilter := 'somevalue if nothing to filter';
    
    SecondTable.SETFILTER(SomeField,TestFilter,'');
    

    and another one:

    TestFilter := '';
    IF SomeTable.FIND('-') then begin
      REPEAT
        if  SomeTable.Field3 = '' then
           FValue := '%1'
        else
           FValue := SomeTable.Field3;
        if TestFilter = '' then
          TestFilter := FValue 
        else
          TestFilter += '|'+FValue; 
      UNTIL SomeTable.NEXT =0
    end else
      TestFilter := 'somevalue if nothing to filter';
    
    SecondTable.SETFILTER(SomeField,TestFilter,'');
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    randrewsrandrews Member Posts: 135
    Thanks a lot, kine, for writing code. It is exactly what i needed :) My code was more biger.

    And thanks to all who helped and writed here
Sign In or Register to comment.