Recent

Author Topic: How to display empty string instead of 30.12.1899 in case of NULL date  (Read 2059 times)

krzynio

  • Full Member
  • ***
  • Posts: 108
    • Krzynio's home page
Hello!

How to display empty string in TDBDateEdit instead of 30.12.1899 in case of NULL or zero date?
30.12.1899 looks inelegant.

Lazarus 3.6 on Windows 11.
Debian 12.8 x64, / Windows 11 PL - latest updates
Lazarus 3.6

Zvoni

  • Hero Member
  • *****
  • Posts: 2791
Re: How to display empty string instead of 30.12.1899 in case of NULL date
« Reply #1 on: November 22, 2024, 01:02:34 pm »
Hello!

How to display empty string in TDBDateEdit instead of 30.12.1899 in case of NULL or zero date?
30.12.1899 looks inelegant.

Lazarus 3.6 on Windows 11.
What's the SQL-Statement looks like?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

paweld

  • Hero Member
  • *****
  • Posts: 1278
Re: How to display empty string instead of 30.12.1899 in case of NULL date
« Reply #2 on: November 22, 2024, 06:00:03 pm »
Use TDBDateTimePicker (the DateTimeCtrls package) instead of TDBDateEdit.
Best regards / Pozdrawiam
paweld

wp

  • Hero Member
  • *****
  • Posts: 12516
Re: How to display empty string instead of 30.12.1899 in case of NULL date
« Reply #3 on: November 22, 2024, 11:53:38 pm »
The attached project demonstrates that TDBEdit is able to display an empty field, but only when the field is null.

TDBDatePicker, on the other hand, has the issue that it ignores an empty string in its TextForNullDate property and displays a "zero" date ("00.00.0000"). This can be fooled, however, by setting TextForNullDate to a space character.

Zoran

  • Hero Member
  • *****
  • Posts: 1886
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: How to display empty string instead of 30.12.1899 in case of NULL date
« Reply #4 on: November 24, 2024, 01:14:27 am »
TDBDatePicker, on the other hand, has the issue that it ignores an empty string in its TextForNullDate property and displays a "zero" date ("00.00.0000"). This can be fooled, however, by setting TextForNullDate to a space character.

This is not an issue, but it is by design and documented:
Quote
If you want empty display, this can be achieved by setting TextForNullDate to one or more space characters.
« Last Edit: November 24, 2024, 01:16:21 am by Zoran »

five

  • New Member
  • *
  • Posts: 28
Re: How to display empty string instead of 30.12.1899 in case of NULL date
« Reply #5 on: November 26, 2024, 08:43:08 pm »

How to display empty string in TDBDateEdit instead of 30.12.1899 in case of NULL or zero date?
30.12.1899 looks inelegant.

If often use Field.OnGetText event to format and control the displayed text for a Tfield.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DataSetDatetimeGetText(Sender: TField; var Text: string;
  2.   DisplayText: Boolean);
  3. begin
  4.   try
  5.     { Display the contents according to the value of the date }
  6.     If Sender.IsNull then
  7.         Text := ''
  8.     else
  9.        Text := Sender.AsString;
  10.  
  11.   except
  12.     { on error set the default text }
  13.     Text := Sender.AsString;
  14.   end;
  15. end;
  16.  
  17.  
  18.  

« Last Edit: November 26, 2024, 09:08:32 pm by five »

krzynio

  • Full Member
  • ***
  • Posts: 108
    • Krzynio's home page
Re: How to display empty string instead of 30.12.1899 in case of NULL date
« Reply #6 on: December 13, 2024, 01:28:12 pm »
Use TDBDateTimePicker (the DateTimeCtrls package) instead of TDBDateEdit.

Thank you!
I think this is the simplest solution. I ran away from it because it wasn't on the standard component palette, and DBDateEdit didn't exist at all. So as soon as it appeared in the basic palette, I replaced all TDBDateTimePickers with DBDateEdit and that surprised me. :)
I'am considering your solution.
Ragards/Pozdrawiam,
Krzysztof
Debian 12.8 x64, / Windows 11 PL - latest updates
Lazarus 3.6

krzynio

  • Full Member
  • ***
  • Posts: 108
    • Krzynio's home page
Re: How to display empty string instead of 30.12.1899 in case of NULL date
« Reply #7 on: December 13, 2024, 01:45:57 pm »
The attached project demonstrates that TDBEdit is able to display an empty field, but only when the field is null.

TDBDatePicker, on the other hand, has the issue that it ignores an empty string in its TextForNullDate property and displays a "zero" date ("00.00.0000"). This can be fooled, however, by setting TextForNullDate to a space character.

Thank you!
I tried with TDBDatePicker and TextForNullDate property but TextForNullDate seems to be missing in Lazarus 3.6 and FPC 3.2.2
Br, Krzysztof
Debian 12.8 x64, / Windows 11 PL - latest updates
Lazarus 3.6

krzynio

  • Full Member
  • ***
  • Posts: 108
    • Krzynio's home page
Re: How to display empty string instead of 30.12.1899 in case of NULL date
« Reply #8 on: December 13, 2024, 01:55:38 pm »

How to display empty string in TDBDateEdit instead of 30.12.1899 in case of NULL or zero date?
30.12.1899 looks inelegant.

If often use Field.OnGetText event to format and control the displayed text for a Tfield.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DataSetDatetimeGetText(Sender: TField; var Text: string;
  2.   DisplayText: Boolean);
  3. begin
  4.   try
  5.     { Display the contents according to the value of the date }
  6.     If Sender.IsNull then
  7.         Text := ''
  8.     else
  9.        Text := Sender.AsString;
  10.  
  11.   except
  12.     { on error set the default text }
  13.     Text := Sender.AsString;
  14.   end;
  15. end;
  16.  
  17.  
  18.  

This looks really fine and worth of implementation not only in this control.
But in this case, for me, it is less comfortable than going back to TDBDateTimePicker.
My only regret is that instead of a nice calendar icon there is just a plain triangle.
Thank you & BR, Krzysztof
« Last Edit: December 13, 2024, 02:06:06 pm by krzynio »
Debian 12.8 x64, / Windows 11 PL - latest updates
Lazarus 3.6

 

TinyPortal © 2005-2018