0

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?

LU RD
  • 34,438
  • 5
  • 88
  • 296
Hackbrew
  • 349
  • 1
  • 10
  • 23
  • Which type of the OrdersTaxRate field in the DB? For example, with Postgres it is impossible to assign value larger then 0.99 to the filed with type `numeric(2,2)` just because (as i remember) first parameter in the type declaration is a total number (including precision) of the digits for values. – Abelisto Oct 07 '14 at 17:21
  • The OrdersTaxRate is a BCD field with 2 decimal places. – Hackbrew Oct 07 '14 at 17:26
  • Unfortunately I am not familiar with Paradox (actually I forgot almost all about them). So, it is not answer but just hint: try those two code snippets: `POE_Data.OrdersTaxRate.AsFloat := 0.1;` and `POE_Data.OrdersTaxRate.AsFloat := 1.0;` If first will work and second will not - you have some troubles with data type. – Abelisto Oct 07 '14 at 17:33
  • And another hint: try to use `POE_Data.OrdersTaxRate.Value` instead of `POE_Data.OrdersTaxRate.AsFloat` – Abelisto Oct 07 '14 at 17:38
  • I tried all the suggestions above, but none of them worked. I checked other records in the database and they seem to have a value of 7.00. – Hackbrew Oct 07 '14 at 20:06
  • Have you tried using `AsBCD` yet? `POE_Data.OrdersTaxRate.AsBCD := StrToBcd(Copy(TaxRateLabel.Caption, 1,1));` – Remy Lebeau Oct 07 '14 at 22:25

2 Answers2

1

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.

CharlieB
  • 11
  • 1
0

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));
adlabac
  • 416
  • 4
  • 12