Lazarus

Programming => General => Topic started by: BSaidus on November 15, 2019, 09:39:39 pm

Title: Issues with TDateTimePicker
Post by: BSaidus on November 15, 2019, 09:39:39 pm
Hello.
I want to use TDateTimePicker in a simple program ( one form ) and retreive a Date from the DateTimePicker construct a Query and post it to database.
But when I put the Value of Date to Null it doesnt work.
I think that the DateTimePicker.Date is never NULL or 0.
How to do that.
Title: Re: Issues with TDateTimePicker
Post by: dsiders on November 15, 2019, 10:46:58 pm
Hello.
I want to use TDateTimePicker in a simple program ( one form ) and retreive a Date from the DateTimePicker construct a Query and post it to database.
But when I put the Value of Date to Null it doesnt work.
I think that the DateTimePicker.Date is never NULL or 0.
How to do that.

Use the NullDate constant for an empty date/time value in the control. You have to decide what gets put into the database. TDBDateTimePicker handles the conversion in its UpdateData method.
Title: Re: Issues with TDateTimePicker
Post by: BSaidus on November 17, 2019, 02:01:08 pm
Thank you @dsider for the advice.
It works .
Title: Re: Issues with TDateTimePicker
Post by: BSaidus on November 19, 2019, 11:29:06 am
The problem persists.
So I will explain more my issue.

I have a function that assigne a Database field of type Datetime with a value from TDateTimePicker (not TDBDateTimePicker)
the function is
Code: Pascal  [Select]
  1. function fQueryAssignVal( oQuery: TQuery;  // the query
  2.     sFName: String; // field name on DB
  3.     fDataType: ftDataType, // type of field, ftstring ....
  4.     fValue: Variant  // Value that will be Assigned.
  5.   ); boolean;
  6. begin
  7.   if (fDataType in [ftDate]) then
  8.   begin
  9.     if ( TDateTime(psValue) <> TDateTime(NullDate) ) then
  10.       psValue := TDate(VarToDateTime(psValue))
  11.     else psValue := Null;
  12.     if (psValue = 0) then
  13.        psValue := NULL;
  14.     //if ( VarToDateTime(psValue) <> NullDate )
  15.     //   psValue:= Trunc(psValue);
  16.   end;
  17.  
  18.  
  19. end;
  20.  
  21. // WHEN CALLING
  22.      // .....  
  23.      fQueryAssignVal( qryPTNS, 'DateNais', ftDate, DtPicker.Date);
  24.     //....
  25.     qryPTNS.Execute;
  26.  
  27.  

if DtPicker Date is not null itworks, but if it is null It gives me Error Conversion.

I do not know what to do really.

Please help.

Title: Re: Issues with TDateTimePicker
Post by: ASerge on November 19, 2019, 11:31:50 pm
So I will explain more my issue.
Code: Pascal  [Select]
  1. function DateTimeToVariant(const Value: TDate): Variant;
  2. begin
  3.   if IsNullDate(Value) then
  4.     Result := Null
  5.   else
  6.     Result := Value;
  7. end;