Hackbrew
Hackbrew

Reputation: 513

Number is out of range error in Delphi XE

I'm using an old Paradox database along with an older Delphi program I inherited and it keeps giving me a Number is out of range error when I hit this statement:

POE_Data.OrdersTaxRate.AsFloat:= StrToFloat(Copy(TaxRateLabel.Caption, 1,1));

The TaxRateLabel.Caption equals 7%, and so the StrToFloat is passing just the 7 character. The TaxRate field is defined in the database as a BCD field with 2 decimal places. I don't see any minimum or maximum values set in the database, so why is this producing the number out of range error?

Upvotes: 0

Views: 2820

Answers (2)

CharlieB
CharlieB

Reputation: 11

I had a similar problem when updating an old C++Builder project. The solution is to copy the Source\data\Data.DB.pas file to your project's code folder and then make the following code change:

procedure TBCDField.SetAsCurrency(Value: Currency);
begin
  if FCheckRange and ((Value < FMinValue) or (Value > FMaxValue)) then
    RangeError(Value, FMinValue, FMaxValue);
//  if FIOBuffer <> nil then
//    TDBBitConverter.UnsafeFrom<System.Currency>(Value, FIOBuffer);
//  SetData(FIOBuffer, False);
  SetData(@Value, False);    // replaces the lines above
end;                         // so can TField.AsBCD

Then add the modified Data.DB.pas file to your project & rebuild your project.

Upvotes: 1

adlabac
adlabac

Reputation: 416

In Delphi XE there is a AsBcd property. You can use it together with StrToBcd function, which converts string to TBcd:

POE_Data.OrdersTaxRate.AsBcd:= StrToBcd(Copy(TaxRateLabel.Caption, 1,1));

Upvotes: 0

Related Questions