Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TdateTimePicker is like a woman... 1

Status
Not open for further replies.

Koos50

Technical User
Aug 16, 2010
7
ZA
Either you love them or you do not understand them...

Hi Everybody. I have a massive problem to understand the coding to use DateTimePicker.
I want to use 2 DateTimePickers for a start and end date selection.I made a sql query that gives ALL dataset results on a Quick report. How do I code these DateTimePickers to choose and result only the data from a Dataset between these 2 dates selected?

Any code would be appreciated to show me how to do this as I cannot get it right.

Thank you very much
 
Please don't get in the habit of expecting this, but I was bored. :)
That said, here you go - First the form:
Code:
object Form3: TForm3
  Left = 305
  Top = 344
  Width = 431
  Height = 260
  Caption = 'Try changing the 2 dates...'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 120
    Top = 56
    Width = 22
    Height = 13
    Caption = 'Start'
  end
  object Label2: TLabel
    Left = 120
    Top = 88
    Width = 19
    Height = 13
    Caption = 'End'
  end
  object Button1: TButton
    Left = 160
    Top = 136
    Width = 75
    Height = 25
    Caption = 'Update'
    TabOrder = 0
    OnClick = Button1Click
  end
  object DatePickStart: TDateTimePicker
    Left = 160
    Top = 48
    Width = 186
    Height = 21
    Date = 40409.654021956020000000
    Time = 40409.654021956020000000
    TabOrder = 1
    OnCloseUp = DatePickStartCloseUp
  end
  object DatePickEnd: TDateTimePicker
    Left = 160
    Top = 80
    Width = 186
    Height = 21
    Date = 40409.654111481480000000
    Time = 40409.654111481480000000
    TabOrder = 2
    OnCloseUp = DatePickEndCloseUp
  end
end

And the code:
Code:
unit TestDatePicker;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls;

type
  TForm3 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    DatePickStart: TDateTimePicker;
    DatePickEnd: TDateTimePicker;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure DatePickEndCloseUp(Sender: TObject);
    procedure DatePickStartCloseUp(Sender: TObject);
  private
    { Private declarations }
    StartDate, EndDate: TDate;
    function GetDatesFromDB: boolean;
    function SaveDatesToDB: boolean;
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

function TForm3.GetDatesFromDB: boolean;
//Get 2 arbitrary dates for testing
//Return TRUE if successful
begin
  StartDate:= StrToDate('06/15/2010');  //arbitrary
  EndDate:= StrToDate('08/15/2010');    //
  try
   //Put your code here to get from SQL query...
    DatePickStart.Date:= StartDate;  //Display the dates in the pickers
    DatePickEnd.Date:= EndDate;
    result:= true
  except
    result:= false
  end
end;

function TForm3.SaveDatesToDB: boolean;
//Post new dates to DB
//Return TRUE if successful
begin
  //Put your code here to put dates into SQL query...
  result:= true
end;

procedure TForm3.DatePickStartCloseUp(Sender: TObject);
begin
  StartDate:= DatePickStart.Date;
  ShowMessage('StartDate changed to ' + DateToStr(StartDate))
end;

procedure TForm3.DatePickEndCloseUp(Sender: TObject);
begin
  EndDate:= DatePickEnd.Date;
  ShowMessage('EndDate changed to ' + DateToStr(EndDate))
end;

procedure TForm3.Button1Click(Sender: TObject);
begin
  SaveDatesToDB;
  Close
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
  GetDatesFromDB; //Init
end;

end.
BTW - I wish women were this simple...

HTH

Roo
Delphi Rules!
 
Thank you very much for this great job roo0047. It is people like you that make life happy for our ordinary people trying to program delphi and battling with some components. Your response was excellent and know that you teached someone something I will always remember and use.
I could not find the exact method in any book or help or site.

Thank you very much.


roo0047 ROCKS!!!
 
Thanks for the vote of confidence! You made me feel much better after getting egg on my face with 'Generics'.

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top