Board index » delphi » "Null" dates

"Null" dates

I am writing an application that has a DateLeft field.

I want the field to be empty, so that nothing displays in a
DateTimePicker when my record is created.

How do I do that?  I've tried DateLeft.AsVariant = NULL, but that gets
displayed as 31/12/1899!

I need to be able to test for the "empty" value in Filters, so that I
can (say) list the people who haven't left by testing this field.

I am using Paradox tables with Delphi 3

Regards

Jeff

 

Re:"Null" dates


On Thu, 27 Aug 1998 16:55:14 +1200, Jeff Cook <je...@aspect.co.nz>
wrote:

Quote
>I want the field to be empty, so that nothing displays in a
>DateTimePicker when my record is created.

TDateTimePicker is not able to do that. You can use the internal
checkbox to indicate whether the date is valid or NULL. If this is not
acceptable, you'll have to get a third-party date editor or write your
own (as I have).

---
Yorai Aminov (TeamB)
http://ourworld.compuserve.com/homepages/yaminov
(TeamB cannot answer questions received via email.
To contact me for any other reason remove nospam from my address)

Re:"Null" dates


Jeff

I believe you can set it like:

 YourFieldName.IsNull := True;

You can test it in the same way:

 if YourFieldName.IsNull then
  <code>

Rick

Quote
>I want the field to be empty, so that nothing displays in a
>DateTimePicker when my record is created.

>How do I do that?  I've tried DateLeft.AsVariant = NULL, but that gets
>displayed as 31/12/1899!

>I need to be able to test for the "empty" value in Filters, so that I
>can (say) list the people who haven't left by testing this field.

>I am using Paradox tables with Delphi 3

>Regards

>Jeff

Re:"Null" dates


YourFieldName.IsNull := True;  // YourFieldName.IsNull is of TDateTimeField
Above gives an error "Cannot assign to a read-only property".

Ashok

Quote
Rick Roen wrote in message <6s66lu$qj...@forums.borland.com>...

>Jeff

>I believe you can set it like:

> YourFieldName.IsNull := True;

>You can test it in the same way:

> if YourFieldName.IsNull then
>  <code>

>Rick

>>I want the field to be empty, so that nothing displays in a
>>DateTimePicker when my record is created.

>>How do I do that?  I've tried DateLeft.AsVariant = NULL, but that gets
>>displayed as 31/12/1899!

>>I need to be able to test for the "empty" value in Filters, so that I
>>can (say) list the people who haven't left by testing this field.

>>I am using Paradox tables with Delphi 3

>>Regards

>>Jeff

Re:"Null" dates


Quote
Ashok Patel wrote:

> YourFieldName.IsNull := True;  // YourFieldName.IsNull is of TDateTimeField
> Above gives an error "Cannot assign to a read-only property".

> Ashok

try:
   YourFieldName.Clear; // sets field to null

taine

Re:"Null" dates


Quote
taine gilliam wrote:

> Ashok Patel wrote:

> > YourFieldName.IsNull := True;  // YourFieldName.IsNull is of TDateTimeField
> > Above gives an error "Cannot assign to a read-only property".

> > Ashok

> try:
>    YourFieldName.Clear; // sets field to null

> taine

Thanks guys and gals

OK, now I've got a date field with null in it and I can test its value
in Pascal, but how can I write a filter that looks for all "Date <>
NULL"

Cheers

Jeff

Re:"Null" dates


As you wrote "Date <> NULL" :)

--
Sincerely yours
Anatoly Podgoretsky
n...@vnet.ee

Jeff Cook ??? a ???? <35E9C2E6.7...@aspect.co.nz> ...

Quote
>taine gilliam wrote:

>> Ashok Patel wrote:

>> > YourFieldName.IsNull := True;  // YourFieldName.IsNull is of
TDateTimeField
>> > Above gives an error "Cannot assign to a read-only property".

>> > Ashok

>> try:
>>    YourFieldName.Clear; // sets field to null

>> taine

>Thanks guys and gals

>OK, now I've got a date field with null in it and I can test its value
>in Pascal, but how can I write a filter that looks for all "Date <>
>NULL"

>Cheers

>Jeff

Re:"Null" dates


That doesn't seem to work for me!

In Pascal  'LeftDate.AsVariant  <> NULL' is OK

But in a filter 'LeftDate <> NULL'  finds all records, including those with
null dates

Jeff

Quote
Anatoly Podgoretsky wrote:
> As you wrote "Date <> NULL" :)

> --
> Sincerely yours
> Anatoly Podgoretsky
> n...@vnet.ee

> Jeff Cook ??? a ???? <35E9C2E6.7...@aspect.co.nz> ...
> >taine gilliam wrote:

> >> Ashok Patel wrote:

> >> > YourFieldName.IsNull := True;  // YourFieldName.IsNull is of
> TDateTimeField
> >> > Above gives an error "Cannot assign to a read-only property".

> >> > Ashok

> >> try:
> >>    YourFieldName.Clear; // sets field to null

> >> taine

> >Thanks guys and gals

> >OK, now I've got a date field with null in it and I can test its value
> >in Pascal, but how can I write a filter that looks for all "Date <>
> >NULL"

> >Cheers

> >Jeff

--
Jeff Cook                                         je...@aspect.co.nz
+------------------------------------------------------------------+
|At work:        Phone: +64-9-424 5388      Fax:    +64-21-785 097 |
|   Program Development/Client Support      Mobile: +64-21-635 185 |
|   Aspect Systems Ltd                                             |
|   Specialists in software for Real Estate and Human Resources    |
+------------------------------------------------------------------+
|At home:                                   Phone:  +64-9-424 0336 |
|   Husband, Dad, Grandad                                          |
|   Enthusiastic but slow triathlete                               |
|   Justice of the Peace                                           |
+------------------------------------------------------------------+

Re:"Null" dates


Hi,
I'd like to contribute with an additional related question:

If I assign a TDateTimeField  to another one via

tblTestMyDate1.Assign(tblTestMyDate2);

it results in <tblTestMyDate1> = 31.12.1899
if <tblTestMyDate2> is empty. This is VERY BAD.
Is there an easy way to prevent this behaviour ???

Richey

On Mon, 31 Aug 1998 17:35:31 +1200, Jeff Cook <je...@aspect.co.nz>
wrote:

Quote
>That doesn't seem to work for me!

>In Pascal  'LeftDate.AsVariant  <> NULL' is OK

>But in a filter 'LeftDate <> NULL'  finds all records, including those with
>null dates

>Jeff

>Anatoly Podgoretsky wrote:

>> As you wrote "Date <> NULL" :)

>> --
>> Sincerely yours
>> Anatoly Podgoretsky
>> n...@vnet.ee

>> Jeff Cook ??? a ???? <35E9C2E6.7...@aspect.co.nz> ...
>> >taine gilliam wrote:

>> >> Ashok Patel wrote:

>> >> > YourFieldName.IsNull := True;  // YourFieldName.IsNull is of
>> TDateTimeField
>> >> > Above gives an error "Cannot assign to a read-only property".

>> >> > Ashok

>> >> try:
>> >>    YourFieldName.Clear; // sets field to null

>> >> taine

>> >Thanks guys and gals

>> >OK, now I've got a date field with null in it and I can test its value
>> >in Pascal, but how can I write a filter that looks for all "Date <>
>> >NULL"

>> >Cheers

>> >Jeff

___________________________________________________
Richard Fellner (emails_to__r.fell...@bigfoot.com)
Find INF-Tool, the *special* setup software or the Resource-Grabber
(collects button images,icons etc. from other EXE/DLL's on your PC)
at www.user.xpoint.at/r.fellner/ ! Or visit the "DELPHI-BOX", one of
the biggest Delphi starting points worldwide (1000+ links and infos) !

Re:"Null" dates


Richey,

I raised this issue back when Delphi 2 was introduced and the TDateTime
behavior was "improved" to be compatible with MS OLE.

We had to write our own date handling routines.

Basically, the TDateField.AsString method is completely reliable.
Therefore, we wrote three routines which attempted to convert the dates
in a consistent and predictable manner.

Direct access to a TDateTime field yielding TDateTime:

{ ===== }
// tests for an empty (null) date in the table
// and replaces it with the 'old' invalid date value
function DateAsDateTime(DateField : TDateField) : TDateTime;
begin

  if TDateField(DateField).AsString = ''
  then
    Result := -693594.0  // documented conversion factor
  else
    Result := StringToDate(TDateField(DateField).AsString);

end;

Correct the Delphi provided date conversion routines:

{ ===== }
// correct the StrToDateTime() routine
// convert a string representation of a date into a TDateTime
// while converting an empty string to the 'old' invalid date value
function StringToDate(DateString : string) : TDateTime;
begin

  if Length(Trim(DateString)) = 0
  then
    Result := -693594.0
  else
  try
    Result := StrToDateTime(DateString);
  except
    Result := -693594.0;
  end;

end;

{ ===== }
// correct the DateToStr() routine
// convert a TDateTime value to a 'valid' string representation
// returning '00/00/0000' isn't acceptable
function DateToString(DateTime : TDateTime) : string;
begin

  Result := '';
  try
    Result := DateToStr(DateTime);
  except
  end;  
  // all zeros means invalid, return an empty string
  // this routine could easily be improved upon to account
  // for any date format, I just didn't take the time
  if (Result = '0/0/0000') or
     (Result = '00/00/0000') or
     (Result = '0/0/00') or
     (Result = '00/00/00')
  then
    Result := ''

end;

The Trim() function is defined as follows:

{---------------------------------------------------------}
function LTrim(s : string) : string;
begin
  Result := '';
  if length(s) > 0
  then
  begin
    while (length(s) > 0) and
          ((s[1] = ' ') or
           (s[1] = chr(9)))
    do
      delete(s,1,1);
    Result := s;
  end;
end;

{---------------------------------------------------------}
function RTrim(s : string) : string;
begin
  Result := '';
  if length(s) > 0
  then
  begin
    while (length(s) > 0) and
          ((s[length(s)] = ' ') or
           (s[length(s)] = chr(9)))
    do
      delete(s,length(s),1);
    Result := s;
  end;
end;

{---------------------------------------------------------}
function Trim(s : string) : string;
begin
  Trim := LTrim(RTrim(s));
end;

Sanity finally returned when using these.

Unfortunately, this is one of those areas where Borland, now Inprise,
has apparently decided that the behavior is acceptable for some reason.
I'd like to see this addressed in the future.  I personally didn't see
the reason why a new TOLEDateTime wasn't introduced in Delphi 2 in order
to implement the changed behavior.  I don't really care today whether
the new values are kept.  I would just like the TDateField.AsDateTime
method and the DateToStr and StrToDate routines to be corrected so they
return reliable, consistent, usable results.

Near future implementations of these procedures will be incorporated
into our TField enhancements, if we can figure out how to get the IDE to
use our TField objects instead of the default ones in the TTable field
editor.  This way, the AsDateTime will return the value we want, and
will store the proper values as well.

For now, we use the above functions, and we only communicate with the
TDateFields using AsString methods, since they are the only ones which
are completely reliable.

Note:  I believe I documented all of the appropriate routines.  Please
let me know if I left something out.

Hope this helps.

--
Mark Richter
eMCee Software

Re:"Null" dates


Hi Mark,

On Sat, 05 Sep 1998 12:13:13 -0500, Mark Richter

Quote
<mrich...@emceesoftware.com> wrote:
>Richey,
>I raised this issue back when Delphi 2 was introduced and the TDateTime
>behavior was "improved" to be compatible with MS OLE.

[many lines follow]

Thanks a lot for this detailled response, Mark.

Best greetings,
Richey
___________________________________________________
Richard Fellner (emails_to__r.fell...@bigfoot.com)
Find INF-Tool, the *special* setup software or the Resource-Grabber
(collects button images,icons etc. from other EXE/DLL's on your PC)
at www.user.xpoint.at/r.fellner/ ! Or visit the "DELPHI-BOX", one of
the biggest Delphi starting points worldwide (1000+ links and infos) !

Other Threads