Mega Code Archive

 
Categories / Delphi / ADO Database
 

How do I incorporate a TDateTimePicker in a Database

Title: How do I incorporate a TDateTimePicker in a Database Question/Problem/Abstract: When filling in date fields it is handy to have a calendar style look-up field. Unfortunately a "tdbCalendarlookUpBox" do not exist (unless it is a 3rd party component), so we have to combine a TdateTimePicker component with a TdataSource Answer: Necessary: 1) Of course a database with a date field 2) a Form 3) a TdateTimepicker Component 4) a Tquery or Ttable 5) a Tdatasource componenent 6) optional a TdbNavigator In my case I have a Date field named ShiftDate in my query Events to be programmed onDataChange, onUpdateData (datasource) onClick (DateTimepicker) This syncronizes the DateTimePicker with the database procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField); begin inherited; DateTimePicker1.Date := Query1SHIFTDATE.Value; end; This syncronizes the database with the DateTimePicker procedure TForm1.DataSource1UpdateData(Sender: TObject); begin inherited; Query1SHIFTDATE.Value; := DateTimePicker1.Date; end; When a date is entered the folowing procedure exports the value to the database procedure TForm1.DateTimePicker1Click(Sender: TObject); begin inherited; //disconnect handler DataSource1.OnDataChange := nil; // set query in edit mode Query1.Edit; //reconnect handler DataSource1.OnDataChange := DataSource1DataChange; end; With above fragment it is possible to make your own VCL component.