Reputation: 513
I'm attempting to update an old Paradox (BDE) table using an older Delphi XE6 application. One particular table has two fields; a Date field, and a BCD field. I'm unable to post a new record to the (FuelSurch) table due to the BCD (FuelSur) field which is throwing a "Number is out of range" error.
So I wrote a quick test, but still the same issue. Here are the FuelSurch table field specifications.
I just added a button to a form and this procedure fires when clicked.
procedure TTestForm.BCDTestBtnClick(Sender: TObject);
var
Bcd: TBcd;
begin
POE_Data.FuelSurch.Open;
POE_Data.FuelSurch.Insert;
Bcd:= StrToBcd('2.01');
showMessage('Bcd = ' + BcdToStr(Bcd)); // This works and shows 'Bcd = 2.01'
POE_Data.FuelSurchFuelSur.AsBCD := Bcd; // Line produces "Number is out of range." error
POE_Data.FuelSurch.Post;
POE_Data.FuelSurch.Close;
end;
The database connection is confirmed as good as I'm able to post to other tables. It just seems like this BCD field type is giving me issues.
Do I need to format the string differently before assigning it to the Bcd variable?
Upvotes: 1
Views: 476
Reputation: 18067
Internally the precision of a BCD is the total number of digits, not the number of digits after the decimal separator.
It's always an even number (when odd, 1 is added to it). The reason is that a byte stores 2 digits.
So if you try this:
procedure TForm1.Button1Click(Sender: TObject);
var
Bcd: TBCD;
begin
Bcd := StrToBcd('2.01', TFormatSettings.Invariant);
ShowMessage(IntToStr(Bcd.Precision));
end;
you will see 4, because 3 digits are used, plus 1 to make it even.
Your field precision is only 2. If it represents the same as TBCD.Precision
, it's not enough.
Note that BcdPrecision()
returns the number of digits before the decimal separator, which is a bit confusing.
Upvotes: 1