MIN MAX?

Hi,

I just want to find out which of the dates is the oldest & which is the youngest

MinDate:=(FromDate,ToDate,FromDate2,ToDate2,FromDate3,ToDate3)GETRANGEMAX

i know this code doesn’t work, but any suggestions?

<M>

Use the Date table, set your filters (you may want to view the fields and their values in a form to see how it works), and do a FINDFIRST or FINDLAST.

Date table??

I couldn’t find any date table

Do you mean accounting period ? or?

The Dates are all global variables.

Is it possible to work out which of the values is the greatest / smallest?

Date, the virtual table. like this picture:

Date table is a virtual table u can’t access virtual tables from object designer .u can use it in reports,codeunit,forms and pages .

http://dynamicsuser.net/forums/p/26474/142896.aspx

http://www.mibuso.com/forum/viewtopic.php?t=4374

If your requirements are literally that simple - find the oldest and newest dates from the list you pass, then I’d probably settle for a simple looping construct to test each date. Either the date you’re inspecting is the earliest, the latest or in the middle.

I would setup this function to take 2 arguments, both date arrays. The first is a 1 x 2 date array that will receive the results - MinMaxDate[1] would be the minimum date, MinMaxDate[2] would be the maximum date. The second argument would be a 10 x 2 array that would contain your source data that you want to inspect. DatesToSearch[1,1] would be FromDate1, DatesToSearch[1,2] would be ToDate1, and so on. Then it’s just a question of looping through the Search array and inspecting the data values.

I’ve put a stub form together to demonstrate the concept. It’s about everything you need.

On the first date you inspect from the passed in list, automatically assign the value of the date to MinMaxDates[1] (being considered the oldest date) and to MinMaxDates[2] (being considered the newest date).

Here’s the thrust of the function. MinMaxDate and DatesToSearch are global date arrays. As you can see, I substituted your argument list of individual To and From Date values with a date array. This gives you the flexibility to add more date pairs to the list without changing the program, and cleans up the looping code very nicely.

Now this function looks for absolute oldest date and absolute newest date, without regard to date pairs on the incoming DatesToSearch array. If your feature requires that you treat the pairs as a unit, then you can tweak the function to handle that.

You got some suggestions dealing with the Date table, and that approach can certainly work. It just seemed to me that this bit of code might be easier to get your arms around.

I hope it helps.

=============================================================
OBJECT Form 60000 Date Sort Function
{
OBJECT-PROPERTIES
{
Date=11/08/11;
Time=[ 3:21:48 AM];
Modified=Yes;
Version List=;
}
PROPERTIES
{
Width=12650;
Height=8470;
Sizeable=No;
OnOpenForm=BEGIN
ResetResults(MinMaxDate);
GenerateRandomDates;
END;

}
CONTROLS
{
{ 1000000002;TextBox;3740 ;330 ;2420 ;440 ;CaptionML=ENU=From Date 1;
SourceExpr=DatesToSearch[1,1] }
{ 1000000003;Label ;330 ;330 ;3300 ;440 ;ParentControl=1000000002 }
{ 1000000005;TextBox;3740 ;880 ;2420 ;440 ;CaptionML=ENU=To Date 1;
SourceExpr=DatesToSearch[1,2] }
{ 1000000006;Label ;330 ;880 ;3300 ;440 ;ParentControl=1000000005 }
{ 1000000007;Label ;330 ;1430 ;3300 ;440 ;ParentControl=1000000008 }
{ 1000000008;TextBox;3740 ;1430 ;2420 ;440 ;CaptionML=ENU=From Date 2;
SourceExpr=DatesToSearch[2,1] }
{ 1000000009;TextBox;3740 ;1980 ;2420 ;440 ;CaptionML=ENU=To Date 2;
SourceExpr=DatesToSearch[2,2] }
{ 1000000010;Label ;330 ;1980 ;3300 ;440 ;ParentControl=1000000009 }
{ 1000000011;Label ;330 ;2530 ;3300 ;440 ;ParentControl=1000000012 }
{ 1000000012;TextBox;3740 ;2530 ;2420 ;440 ;CaptionML=ENU=From Date 3;
SourceExpr=DatesToSearch[3,1] }
{ 1000000013;TextBox;3740 ;3080 ;2420 ;440 ;CaptionML=ENU=To Date 3;
SourceExpr=DatesToSearch[3,2] }
{ 1000000014;Label ;330 ;3080 ;3300 ;440 ;ParentControl=1000000013 }
{ 1000000015;TextBox;10010;6710 ;2420 ;440 ;Editable=No;
CaptionML=ENU=Maximum date;
SourceExpr=MinMaxDate[2] }
{ 1000000016;Label ;6600 ;6710 ;3300 ;440 ;ParentControl=1000000015 }
{ 1000000017;Label ;6600 ;6160 ;3300 ;440 ;ParentControl=1000000018 }
{ 1000000018;TextBox;10010;6160 ;2420 ;440 ;Editable=No;
CaptionML=ENU=Minimum Date;
SourceExpr=MinMaxDate[1] }
{ 1000000019;MenuButton;10230;7590;2200;550 ;HorzGlue=Right;
VertGlue=Bottom;
CaptionML=ENU=&Functions;
Menu=MENUITEMS
{
{ ID=1000000020;
CaptionML=ENU=Get Min & Max Dates;
OnPush=BEGIN
GetMinMaxDates(MinMaxDate, DatesToSearch);
END;
}
{ ID=1000000021;
CaptionML=ENU=Clear Dates;
OnPush=BEGIN
ClearInputData;
END;
}
{ ID=1000000047;
CaptionML=ENU=Generate Random Dates;
OnPush=BEGIN
GenerateRandomDates;
END;
}
}
OnActivate=BEGIN
ClearInputData;
END;
}
{ 1000000000;TextBox;3740 ;5390 ;2420 ;440 ;CaptionML=ENU=To Date 5;
SourceExpr=DatesToSearch[5,2] }
{ 1000000001;Label ;330 ;5390 ;3300 ;440 ;ParentControl=1000000000 }
{ 1000000004;Label ;330 ;4840 ;3300 ;440 ;ParentControl=1000000022 }
{ 1000000022;TextBox;3740 ;4840 ;2420 ;440 ;CaptionML=ENU=From Date 5;
SourceExpr=DatesToSearch[4,1] }
{ 1000000023;TextBox;3740 ;4290 ;2420 ;440 ;CaptionML=ENU=To Date 4;
SourceExpr=DatesToSearch[4,2] }
{ 1000000024;Label ;330 ;4290 ;3300 ;440 ;ParentControl=1000000023 }
{ 1000000025;Label ;330 ;3740 ;3300 ;440 ;ParentControl=1000000026 }
{ 1000000026;TextBox;3740 ;3740 ;2420 ;440 ;CaptionML=ENU=From Date 4;
SourceExpr=DatesToSearch[4,1] }
{ 1000000027;TextBox;10010;5390 ;2420 ;440 ;CaptionML=ENU=To Date 10;
SourceExpr=DatesToSearch[10,2] }
{ 1000000028;TextBox;10010;4840 ;2420 ;440 ;CaptionML=ENU=From Date 10;
SourceExpr=DatesToSearch[10,1] }
{ 1000000029;TextBox;10010;4290 ;2420 ;440 ;CaptionML=ENU=To Date 9;
SourceExpr=DatesToSearch[9,2] }
{ 1000000030;TextBox;10010;3740 ;2420 ;440 ;CaptionML=ENU=From Date 9;
SourceExpr=DatesToSearch[9,1] }
{ 1000000031;TextBox;10010;3080 ;2420 ;440 ;CaptionML=ENU=To Date 8;
SourceExpr=DatesToSearch[8,2] }
{ 1000000032;TextBox;10010;2530 ;2420 ;440 ;CaptionML=ENU=From Date 8;
SourceExpr=DatesToSearch[8,1] }
{ 1000000033;TextBox;10010;1980 ;2420 ;440 ;CaptionML=ENU=To Date 7;
SourceExpr=DatesToSearch[7,2] }
{ 1000000034;TextBox;10010;1430 ;2420 ;440 ;CaptionML=ENU=From Date 7;
SourceExpr=DatesToSearch[7,1] }
{ 1000000035;TextBox;10010;880 ;2420 ;440 ;CaptionML=ENU=To Date 6;
SourceExpr=DatesToSearch[6,2] }
{ 1000000036;Label ;6600 ;880 ;3300 ;440 ;ParentControl=1000000035 }
{ 1000000037;Label ;6600 ;1430 ;3300 ;440 ;ParentControl=1000000034 }
{ 1000000038;Label ;6600 ;1980 ;3300 ;440 ;ParentControl=1000000033 }
{ 1000000039;Label ;6600 ;2530 ;3300 ;440 ;ParentControl=1000000032 }
{ 1000000040;Label ;6600 ;3080 ;3300 ;440 ;ParentControl=1000000031 }
{ 1000000041;Label ;6600 ;5390 ;3300 ;440 ;ParentControl=1000000027 }
{ 1000000042;Label ;6600 ;4840 ;3300 ;440 ;ParentControl=1000000028 }
{ 1000000043;Label ;6600 ;4290 ;3300 ;440 ;ParentControl=1000000029 }
{ 1000000044;Label ;6600 ;3740 ;3300 ;440 ;ParentControl=1000000030 }
{ 1000000045;TextBox;10010;330 ;2420 ;440 ;CaptionML=ENU=From Date 6;
SourceExpr=DatesToSearch[6,1] }
{ 1000000046;Label ;6600 ;330 ;3300 ;440 ;ParentControl=1000000045 }
}
CODE
{
VAR
DatesToSearch@1000000000 : ARRAY [10,2] OF Date;
MinMaxDate@1000000001 : ARRAY [2] OF Date;
Year@1000000002 : Integer;
Month@1000000003 : Integer;
Day@1000000004 : Integer;
MaxArrayLen@1000000005 : Integer;
i@1000000006 : Integer;
x@1000000007 : Integer;
seed@1000000008 : Integer;

PROCEDURE [GetMinMaxDates@1000000000(VAR](mailto:GetMinMaxDates@1000000000(VAR) MinMaxDate@1000000006 : ARRAY [2] OF Date;DatesToSearch@1000000000 : ARRAY [10,2] OF Date);
VAR
MaxArrayIndex@1000000007 : Integer;
i@1000000001 : Integer;
x@1000000002 : Integer;
BEGIN
MaxArrayIndex := ARRAYLEN(DatesToSearch,1);
ResetResults(MinMaxDate);

// ignore blank values in the search array
IF DatesToSearch[1,1] > 0D THEN BEGIN
MinMaxDate[1] := DatesToSearch[1,1];
MinMaxDate[2] := DatesToSearch[1,1];
END;

// priming read
IF DatesToSearch[1,2] > 0D THEN BEGIN
IF MinMaxDate[1] > DatesToSearch[1,2] THEN
MinMaxDate[1] := DatesToSearch[1,2];
IF MinMaxDate[2] < DatesToSearch[1,2] THEN
MinMaxDate[2] := DatesToSearch[1,2];
END;

FOR i := 2 TO MaxArrayIndex DO BEGIN
FOR x := 1 TO 2 DO BEGIN
IF DatesToSearch[i,x] > 0D THEN BEGIN
IF MinMaxDate[1] > DatesToSearch[i,x] THEN
MinMaxDate[1] := DatesToSearch[i,x];
IF MinMaxDate[2] < DatesToSearch[i,x] THEN
MinMaxDate[2] := DatesToSearch[i,x];
END;
END;
END;
END;

PROCEDURE [ResetResults@1000000002(VAR](mailto:ResetResults@1000000002(VAR) MinMaxDate@1000000000 : ARRAY [2] OF Date);
BEGIN
CLEAR(MinMaxDate);
END;

PROCEDURE ClearInputData@1000000006();
BEGIN
CLEAR(DatesToSearch);
ResetResults(MinMaxDate);
END;

PROCEDURE GenerateRandomDates@1000000030();
BEGIN
MaxArrayLen := ARRAYLEN(DatesToSearch,1);
FOR i := 1 TO MaxArrayLen DO BEGIN
RANDOMIZE; FOR seed := 1 TO 35000 DO;
FOR x := 1 TO 2 DO BEGIN
RANDOMIZE;FOR seed := 1 TO 415000 DO;
Year := RANDOM(12) + 2000;
RANDOMIZE;FOR seed := 1 TO 525000 DO;
Month := RANDOM(12);
RANDOMIZE;FOR seed := 1 TO 635000 DO;
Day := RANDOM(27);
DatesToSearch[i,x] := DMY2DATE(Day,Month,Year);
END;
END;
END;

BEGIN
END.
}
}