StartsWith-Filter

DosihrisDosihris Member Posts: 23
Hi,

is there a way to find out whether a name starts with "Ha"?
i have a boolean variable "isrightname" and i want to do something like:

isrightname := Name.StartsWith("Ha");

or

isrightname := Name = 'Ha*';

Is there something like that?

Comments

  • kinekine Member Posts: 12,562
      MyTempTable.Field := 'Haramadaka';
      MyTempTable.INSERT;
      MyTempTable.SETFILTER(Field,'%1*','Ha');
      isrightname := not MyTempTable.ISEMPTY;
    
    8)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • garakgarak Member Posts: 3,263
    edited 2009-09-18
    or, if you loop through a table where the Names are stored and you want in these table set the boolean "isrightname", then u can use also this:
    MyTable.setcurrentkey(YourNeededKey);
    MyTable.setrange("Needed Field","NeededValue");
    if MyTable.findset(true) then begin
      repeat     
        if  (Strlen(MyTable.FieldToCheck) >=2) then begin                                                //Uppercase,Lowercase
          MyTable.isrightname := (MyTable.FieldToCheck[1] in [72,104]) and (MyTable.FieldToCheck[2] in [65,97]);
          MyTable.modify; 
          //also possible is to use a string function like strpos()
        end;
      until MyTable.next = 0;
    end;
    

    if you will set it only on a variable on the form, and u diesn't save the variable value, u can use:
    isrightname := (FieldToCheck[1] in [72,104]) and (FieldToCheck[2] in [65,97]);
    
    or
    MyTableasVariable.setrange(PromaryKeyField,PrimarykeyOftheRecFromtheCard);
    MyTableasVariable.setfilter("FieldToCheck",'%1','Ha*') ; //also possible with the @ infront if the Ha
    isrightname :=  not MyTableasVariable.isempty;
    

    Regards
    Do you make it right, it works too!
  • MBergerMBerger Member Posts: 413
    garak wrote:
    or, if you loop through a table where the Names are stored and you want in these table set the boolean "isrightname", then u can use also this:
    ...
    
    Or just
    MyTable.reset ;
    Mytable.modifyall(IsRightname,true) ;
    MyTable.setfilter("Needed Field",'@%1*','Ha') ; // Leave out the @ for case-sensitive
    Mytable.modifyall(IsRightname,true) ;
    
  • garakgarak Member Posts: 3,263
    are u sure that this will work?
    MBerger wrote:
    MyTable.reset ;
    Mytable.modifyall(IsRightname,true) ; 
    MyTable.setfilter("Needed Field",'@%1*','Ha') ; // Leave out the @ for case-sensitive
    Mytable.modifyall(IsRightname,true) ;
    

    The first modify: Mytable.modifyall(IsRightname,true) ; <-- u modify all recs in the table

    MyTable.setfilter("Needed Field",'@%1*','Ha') ; <-- i mean i must be: MyTable.setfilter("Needed Field",'%1','@Ha*') ;

    ;-)

    Regards
    Do you make it right, it works too!
  • DosihrisDosihris Member Posts: 23
    Thank you guys for that interesting solutions. i never thought that i have to redevelop half NAV to just get this simple Filter in the text field.

    But many of you advised SETFILTER. Well, let me shortly explain what i want to do and than i think you know that SETFILTER is just not really possible.

    I use NAV 2009 RTC, the contact list. I have set a style to the name column (so all the names in the list are styled green.) But i dont want that all names are green, i only want names green that starts with "Hans". So there is a StyleExpr field, where you can set a boolean expression. In MSDN i read that i ofcourse cant just write in that field "Name = 'Hans*'". I have to set a global boolean variable and in the function "OnAfterGetRecord" i have to evaluate every single name and find out whether it starts with hans. If so i have to set this global variable to true, otherwise to false. and this global variable i can use in StyleExpr. So SETFILTER is not helpfull for me.

    All this works when i write isrightname := Name = 'Hans Viller';

    But than only Hans Viller is styled. But i need all Hanses... :-)
  • MBergerMBerger Member Posts: 413
    garak wrote:
    are u sure that this will work?

    The first modify: Mytable.modifyall(IsRightname,true) ; <-- u modify all recs in the table

    MyTable.setfilter("Needed Field",'@%1*','Ha') ; <-- i mean i must be: MyTable.setfilter("Needed Field",'%1','@Ha*') ;

    ;-)

    Regards
    Whoops....Copy-Paste error. That was meant to set all the records to FALSE.
  • kinekine Member Posts: 12,562
    Dosihris wrote:
    Thank you guys for that interesting solutions. i never thought that i have to redevelop half NAV to just get this simple Filter in the text field.

    But many of you advised SETFILTER. Well, let me shortly explain what i want to do and than i think you know that SETFILTER is just not really possible.

    I use NAV 2009 RTC, the contact list. I have set a style to the name column (so all the names in the list are styled green.) But i dont want that all names are green, i only want names green that starts with "Hans". So there is a StyleExpr field, where you can set a boolean expression. In MSDN i read that i ofcourse cant just write in that field "Name = 'Hans*'". I have to set a global boolean variable and in the function "OnAfterGetRecord" i have to evaluate every single name and find out whether it starts with hans. If so i have to set this global variable to true, otherwise to false. and this global variable i can use in StyleExpr. So SETFILTER is not helpfull for me.

    All this works when i write isrightname := Name = 'Hans Viller';

    But than only Hans Viller is styled. But i need all Hanses... :-)

    My solution is quick - you just enter one line into temporary table and see if the value is inside your filter. This will return you the TRUE-FALSE you need. If you create function for that, you can use it in OnAfterFormat trigger to know if you need to color the name or not. Where is problem? ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • PeterDPeterD Member Posts: 66
    What about:
    IsRightName := (COPYSTR(Name, 1, 4) = 'Hans');
    
  • kinekine Member Posts: 12,562
    :-)

    It is simple... :whistle:

    That's true that in this case no more is needed. My example was originally for more complex filters...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • DosihrisDosihris Member Posts: 23
    PeterD's version is the best. The the way with the temporary table is ok, but i need another global variable. I just want it as simple as possible.

    Thank you guys.
  • SPost29SPost29 Member Posts: 148
    this way you could make Fritz green too instead of only Hans.
    also what you match could be longer
    leave out the lowercase function calls if you need to match case

    DoesStartWith(startswith : Text[10];Name : Text[30]) : Boolean
    exit(lowercase(copystr(name,1,strlen(startswith))) = lowercase(startswith));

    Steve
Sign In or Register to comment.