Jump to content
Lainkes

DateTimePicker - Set empty date

Recommended Posts

Posted (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

2024-04-15 23_07_47-Window.png

Edited by Lainkes

Share this post


Link to post

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
Posted (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 by Remy Lebeau

Share this post


Link to post

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).

2024-04-16 08_49_40-Window.png

Share this post


Link to post

I don't know the Konopka components. But:

  1. In the second part of the code (else) you don't have a value for the date
  2. Some components have the property "Allow null value"

Share this post


Link to post

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

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

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. 

2024-04-16 11_19_35-Window.png

2024-04-16 11_19_25-Window.png

Share this post


Link to post

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
Posted (edited)

It seems that it was not in edit mode after all.

My mistake.

Edited by Lainkes

Share this post


Link to post

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×