Hi Cristian, Thank you! How do I call it earlier? I am using the vanilla TDBGrid. I believe the OnValidate is triggered when I move the cursor to the next field or the cell below in the same field. I modified your code slightly as below:
procedure TfrmClose.tbCloseAmtValidate(Sender: TField);
begin
if Sender = nil then
exit;
try
if (Sender.DataSet.FieldByName('Long Short').AsString = 'Long') then
begin
if (Sender.AsFloat < 0) then
begin
ShowMessage('Close value should be positive or zero');
raise Exception.Create('Wrong value');
end;
end
else if (Sender.DataSet.FieldByName('Long Short').AsString = 'Short') then
begin
if (Sender.AsFloat > 0) then
begin
ShowMessage('Close value should be negative or zero');
raise Exception.Create('Wrong value');
end;
end;
except
on E : Exception do
begin
if E.Message = 'Wrong value' then
Abort;
end;
end;
end;
The cursor stays in the same field (which I want), the the DBGrid copies the value across all records. This is fixed if I correct the value, but looks weird.
When the error is caught, cursor stays in the same cell, but wrong value displayed across all records
When the correct value is entered, the DBGrid updates properly and everything works
I'm just afraid about the problematic wrong value displayed when abort is called. Is there anyway to fix that?
Thank you again Cristian. I am so glad I joined this group!