Delphi Dev Express TcxDBTimeEdit and how to handle nulls ?
I’m trying to use Dev Express TcxDBTimeEdit component in Delphi and I’m keen for user to be able to use mouse to select all and delete key to clear and for this to store a null in the associated database column field value.
I’m not sure how to achieve this.
I have tried this, but it doesn’t work, in the component Exit event:
if
(cxDBTimeEdit1.Text = '00:00 AM')
or (cxDBTimeEdit1.Text = '12:00 AM')
or (cxDBTimeEdit1.Text = '')
then dmClaimLine.qryClaimLineTimeOfService.AsString:='';
I’m also using an old version of Delphi .. Delphi 2007 and also old version of Dev Express components .. version 6.50 from April 2009.
Am connecting via Devart SDAC 8.2.9 to MS SQL Server 2017 DB
I did finally get this working with some quite unusual events code:
procedure TfrmC420201ClaimLine.tedTimeOfServiceKeyDown(Sender: TObject;
var Key: Word; Shift: TShiftState);
begin
inherited;
//This code allows the operator to select the time and press the delete key to return the DB col value to NULL
//The SetFocus on another control and unassign and then reassign DataField is required to achieve this. Looks odd but works.
if Key = VK_DELETE then
begin
tedTimeOfService.DataBinding.DataField:='';
with dmClaimLine do
begin
if (FMode <> 'Assess') then Exit;
ClaimLineEditable;
dmClaimLine.qryClaimLineTimeOfService.Clear;
dmClaimLine.qryClaimLineTimeOfService.AsString:='';
end;
edtMediTimeDuration.SetFocus;
tedTimeOfService.DataBinding.DataField:='TimeOfService';
end;
end;
procedure TfrmC420201ClaimLine.tedTimeOfServiceEnter(Sender: TObject);
begin
inherited;
with dmClaimLine do
begin
if (FMode <> 'Assess') then Exit;
ClaimLineEditable;
dmClaimLine.qryClaimLineTimeOfService.AsDateTime := tedTimeOfService.Time
end;
end;
procedure TfrmC420201ClaimLine.tedTimeOfServicePropertiesChange(Sender: TObject);
var EditValueStr, TimeStr: string;
begin
inherited;
if VarIsNull(tedTimeOfService.EditValue) then EditValueStr := 'Null' else EditValueStr := TimeToStr(tedTimeOfService.Time);
if VarIsNull(tedTimeOfService.Time) then TimeStr := 'Null' else TimeStr := TimeToStr(tedTimeOfService.Time);
with dmClaimLine do
begin
if (FMode <> 'Assess') then Exit;
if (dmClaimLine.qryClaimLineTimeOfService.AsString = '')
and (EditValueStr = 'Null') then exit;
if dmClaimLine.qryClaimLineTimeOfService.AsDateTime <> tedTimeOfService.Time
then
begin
ClaimLineEditable;
dmClaimLine.qryClaimLineTimeOfService.AsDateTime := tedTimeOfService.Time
end;
end;
end;
4
Your code is working directly with the control state instead of the database state.
You should instead try to handle things using the actual underlying field in the underlying dataset.
The fact that your control is a cxDBTimeEdit is not even important to your question.
dmClaimLine.qryClaimLineTimeOfService.AsString:='';
^^ Whatever you think this does, it probably doesn’t do it.^^
If you are using a dataset to directly edit row values you need to specify how you’re doing that. how you do that differs depending on the component set you use to access a database, and the database you’re connecting to, you haven’t specified either one.
You’re coding against the visual controls [cxDBTimeEdit1.Text], when you should be reading the field values from the database access components (DAC). If you were using ADO, for instance, and you had an ADOTable named ClaimLine, you might be needing to do this
ClaimLine.Edit;
ClaimLineTimeOfService.Clear; // make a field null
ClaimLine.Post;
If you were instead updating with an SQL update statement you might do something like this
UpdateClaimEntry.ParamByName('timeofservice').Clear;
UpdateClaimEntry.ExecSQL;
4