Lainkes 0 Posted April 15 (edited) Hallo, I want to assign a null value to a DateTimePicker when a checkbox is checked to tell the user that no date is needed. So I assign the value NULL (found in some other internet topics) that will be converted to '30/12/1899'. DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Date := Null; I get an error message when this code is executed. See attached image. To be complete, this is a DBDateTimePicker (Konopka) that is linked with my datasource. So the meaning is to write directly a null value to the database when hitting the save button on my form. Any idea what I'm doing wrong? Tips and tricks are welcome. Greetings Alain Edited April 15 by Lainkes Share this post Link to post
Uwe Raabe 2057 Posted April 15 The error message is pretty clear: Set the ShowCheckBox property of the DateTimePicker to True. You don't even have to set the Date property to NULL. It should be sufficient to uncheck the Checkbox or set the Checked property to False. That will write a NULL value to the data field instead of 30/12/1899. Share this post Link to post
Remy Lebeau 1396 Posted April 16 (edited) A DateTimePicker does not have a concept of a NULL date/time. That is why it has an optional checkbox instead. If you need the date/time value to be optional, then enable the check box, and if checked then use the date/time otherwise ignore it. You can't assign a NULL date/time value, so just uncheck the box and set a default value if needed. Edited April 16 by Remy Lebeau Share this post Link to post
Lainkes 0 Posted April 16 Thanks for the info. I'll try to make it more specific. I activated the ShowCheckBox property. I have a DBCheckbox where the user can indicate if more info is needed. (check or uncheck) See screenshot. When the DBCheckbox is not checked, the datetimepicker component must be unvisible. And the date in the database must be set to a value that is not usable (like 30/12/1899). If checked, the datetimepicker must be visible and the selected date must be saved. This is my code. procedure Tfrm_Page_Control.DBCheckBox44Click(Sender: TObject); begin if TDBCheckBox(sender).Checked then begin // write selected date to DB : OK DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Checked := True; DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Visible := True; DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Date := Now; end else begin // write unusable date to DB : NOT OK DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Checked := False; DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Visible := False; end; end; But when I uncheck the DBCheckbox, nothing changes in my DB. The last saved date is still available. Thanks for your feedback and help. (or alternative solution). Share this post Link to post
Stano 143 Posted April 16 I don't know the Konopka components. But: In the second part of the code (else) you don't have a value for the date Some components have the property "Allow null value" Share this post Link to post
Lainkes 0 Posted April 16 I added a line with the lowest date possible, but it's not working. I'm sure it's something stupid, but no idea what it is. if TDBCheckBox(sender).Checked then begin // write selected date to DB : OK DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Checked := True; DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Visible := True; DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Date := Now; end else begin // write unusable date to DB : NOT OK DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Date := strToDAte('30/12/1899'); DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Checked := False; DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Visible := False; end; Share this post Link to post
Uwe Raabe 2057 Posted April 16 It may be better to directly change the underlying data field instead of the control: procedure Tfrm_Page_Control.DBCheckBox44Click(Sender: TObject); begin DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.DataSource.Edit; if TDBCheckBox(sender).Checked then begin DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Field.AsDateTime := Now; DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Visible := True; end else begin DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Field.Clear; DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Visible := False; end; end; Share this post Link to post
Lainkes 0 Posted April 16 Thanks. But now I get an error on the line DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Field.AsDateTime := Now; when opening my form. Share this post Link to post
Uwe Raabe 2057 Posted April 16 That error implies that the underlying field is not existing, probably because the dataset is not open. I would expect that the line DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.DataSource.Edit; I added to the code would cause an error first, but perhaps you just missed to copy that line into your code. Anyway, besides checking for Field <> nil, the question is: Why would you want to set that value when the dataset is not open? Share this post Link to post
Lainkes 0 Posted April 16 (edited) It seems that it was not in edit mode after all. My mistake. Edited April 16 by Lainkes Share this post Link to post
Stano 143 Posted April 16 If an array of type Date has the option "null" in the DBTable so DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Field.AsDateTime := 0;// = strToDAte('30/12/1899') You can set DTP_STAT_HAND_GRIF_VOL_DOSSIER_OPVR_DATE.Field.Value := null; Share this post Link to post
Remy Lebeau 1396 Posted April 16 8 hours ago, Lainkes said: I have a DBCheckbox where the user can indicate if more info is needed. (check or uncheck) See screenshot. When the DBCheckbox is not checked, the datetimepicker component must be unvisible. And the date in the database must be set to a value that is not usable (like 30/12/1899). If checked, the datetimepicker must be visible and the selected date must be saved. Then there is no point in enabling the CheckBox on the DateTimePicker itself. Share this post Link to post