Get Current month as filter

andenanden Member Posts: 6
Hello everybody

I am having some difficulties with filters in my C/AL code.I have utilized CU1 and the makedatefilter, but e.g. 'CM' is not accepted by it...

I'm wondering if there is any other way to achieve this - and how can I be able to i.e. go to the month after the month i have in my current filter?

I hope that some one here can provide me with the golden answer.

Answers

  • KYDutchieKYDutchie Member Posts: 345
    Maybe this piece of code will put you on the right track:
    this will filter on the month after your current month.
    yourdate := WORKDATE;
    startdate := CALCDATE('+1M', yourdate);
    enddate := CALCDATE('+2M',yourdate);
    enddateDay := 1;
    enddatemonth := DATE2DMY(enddate,2);
    enddateyear := DATE2DMY(enddate,3);
    enddate := DMY2DATE(enddateday,enddatemonth,enddateyear);
    enddate := CALCDATE('-1D',enddate);
    startdateday := 1;
    startdatemonth := DATE2DMY(startdate,2);
    startdateyear := DATE2DMY(startdate,3);
    Startdate := DMY2DATE(startdateday,startdaymonth,startdayyear);
    
    yourtable.SETRANGE(yourdatevariable,startdate,enddate);
    

    hope this helps,

    Willy
    Fostering a homeless, abused child is the hardest yet most rewarding thing I have ever done.
  • DaveTDaveT Member Posts: 1,039
    Hi,

    .... or you can use the virtual table date 2000000007, type Month and filter/find by date.
    Dave Treanor

    Dynamics Nav Add-ons
    http://www.simplydynamics.ie/Addons.html
  • garakgarak Member Posts: 3,263
    There are some way's.

    for example using calcdate:
    message(format(CALCDATE('<-CM>',today)) + '..' + format(calcdate('<CM>',today)));
    

    Or using the virtual table date:
    Date.reset;
    Date.setrange("Period Type",Date."Period Type"::Month);
    Date.setfilter("Period Start",'<=%1',today);
    Date.findlast;
    message(format(Date."Period Start") + '..' + format(Date."Period End"));
    

    Regards
    Do you make it right, it works too!
  • DenSterDenSter Member Posts: 8,305
    So to elaborate a little on that:
    CALCDATE('<-CM>',TODAY) will get you the FIRST date of the month in TODAY
    CALCDATE('<CM>',TODAY) will get you the LAST date of the month for TODAY

    So to get this month's first/last days and next month's first/last days:
    ThisMonthFirst := CALCDATE('<-CM>',today);
    ThisMonthLast := CALCDATE('<CM>',ThisMonthFirst);
    NextMonthFirst := CALCDATE('<+1D>,ThisMonthLast);
    NextMonthLast := CALCDATE('<CM>',NextMonthFirst);
    
    No need to get into the date table for this particular one in my opinion, although that would also be a very good solution. If you want to get more generic though, the date table is very handy for date processing. You should definately look at it and see how it works, which options are available. Create a new form, put all the fields on there and filter on it. Take a look at the Base Calendar logic in base NAV to see some examples of how it is used.
  • andenanden Member Posts: 6
    Thank you all for your inputs, and thank you DenSter and Garak for the help, it works like a charm. =D>

    Again, thank you


    Best regards
    Anden
  • garakgarak Member Posts: 3,263
    Please, and welcome to mibuso ;-)
    Do you make it right, it works too!
Sign In or Register to comment.