BCS Filter Records By Date Range


There are those times when data records need to be selected based on date fields. The following code snippet indicates how to filter records for a specific day.
[codesyntax lang=”delphi”]

{*-----------------------------------------------------------------------------
  Procedure: Daily1Click
  Date:      15-Dec-2013
  @Param     Sender: TObject
  @Return    None
-----------------------------------------------------------------------------}
procedure TBCSTasksC.Daily1Click(Sender: TObject);
var
  rdt: TDateTime;
  ft, et: string;
begin
  BCSGetDateTimeCmp1.SelDateTime := Now;
  if BCSGetDateTimeCmp1.Execute then
  begin
    rdt := BCSGetDateTimeCmp1.SelDateTime;
    ft := FormatDateTime('yyyy-mm-dd', rdt) + ' 00:00:00';
    et := FormatDateTime('yyyy-mm-dd', rdt) + ' 23:59:59';
    BCSTasksdm.atattd.Filter := '((sdt >=' + '''' + ft + '''' + ') and (edt <='
      + '''' + et + '''' + '))';
    BCSTasksdm.atattd.Filtered := True;
  MemView := BCSTasksdm.frxReport1.FindObject('Memo4') as TfrxMemoView;
  MemView.Text := 'Category ' + FormatDateTime('dddd, mmmm dd, yyyy', rdt);
  end;
end;

[/codesyntax]
Notice the date format for MySQL is yyyy-mm-dd hh:mm:ss.  There is a starting date and time and an ending date and time.  The filter selects records between the date time ranges.
To select the records for a specific week the following code snippet should be adhered to.
[codesyntax lang=”delphi”]

{*-----------------------------------------------------------------------------
  Procedure: Weekly1Click
  Date:      15-Dec-2013
  @Param     Sender: TObject
  @Return    None
-----------------------------------------------------------------------------}
procedure TBCSTasksC.Weekly1Click(Sender: TObject);
var
  fd, ld: String;
  rdt: TDateTime;
begin
  BCSGetDateTimeCmp1.SelDateTime := Now;
  if BCSGetDateTimeCmp1.Execute then
  begin
    rdt := BCSGetDateTimeCmp1.SelDateTime;
    BCSXE3UtilsCmp1.FirLasDow(rdt);
    fd := FormatDateTime('yyyy-mm-dd hh:mm:ss', BCSXE3UtilsCmp1.FirstDayOfWeek);
    ld := FormatDateTime('yyyy-mm-dd hh:mm:ss', BCSXE3UtilsCmp1.LastDayOfWeek);
    BCSTasksdm.atattd.Filter := '((sdt >= ' + '''' + fd + '''' + ') and (edt <='
      + '''' + ld + '''' + '))';
    BCSTasksdm.atattd.Filtered := True;
      MemView := BCSTasksdm.frxReport1.FindObject('Memo4') as TfrxMemoView;
  MemView.Text := 'Category ' +
  FormatDateTime('dddd, mmmm dd, yyyy', BCSXE3UtilsCmp1.FirstDayOfWeek) +
  ' Thru ' +
  FormatDateTime('dddd, mmmm dd, yyyy', BCSXE3UtilsCmp1.LastDayOfWeek);
  end;
end;

[/codesyntax]
The date ranges span midnight on Monday through midnight on Sunday.
Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

Leave a Reply

Your email address will not be published. Required fields are marked *