Recent

Author Topic: DBEdit - display setting of number (IBX)  (Read 1925 times)

Nicole

  • Hero Member
  • *****
  • Posts: 970
DBEdit - display setting of number (IBX)
« on: June 07, 2023, 08:01:56 pm »
There is a TDBEdit, which allows me to modify a value in my database.
This value is defined there as double.
I change the value within the DBEdit from 1 to 1.05.

My DBEdit lets me key in the 1,05 and it changes as it should.
However on "commit" it displays 1,1.
The value in the database is changed correctly to 1.05.

What can I do, that my TBEdit displays the correct value of 1.05 instead of 1.1?

Thanks.

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: DBEdit - display setting of number (IBX)
« Reply #1 on: June 07, 2023, 09:29:46 pm »
You need to change the TField.Displayformat to include 2 decimals.

https://lazarus-ccr.sourceforge.io/docs/fcl/db/tnumericfield.displayformat.html

For example.
Code: Pascal  [Select][+][-]
  1. Query.FieldByName('yourfield').DisplayFormat := '#.00';

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: DBEdit - display setting of number (IBX)
« Reply #2 on: June 08, 2023, 05:36:25 pm »
Thank you for your answer, I searched for something like this, - but cannot find it.
DBEdit has nothing like Field or Display in the Object inspector.
I was told before, that there is a living without the Object Inspector, but as simple girl, I cannot reproduce any more.

I found further a datasource - dataset-query which I (really me?) seemed to have linked to my DBEdit hundred years ago.
But there is not DisplayFormat neither.

So in short: How can I get to this Display Format starting from the DBEdit?
Thanks.


rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: DBEdit - display setting of number (IBX)
« Reply #3 on: June 08, 2023, 05:44:34 pm »
So in short: How can I get to this Display Format starting from the DBEdit?
You need to set it in code.
Or, if you have the fields defined, you can set it in the object inspector.

How did you set the field in dbedit?

You can put the code I gave directly below the line where you call open on the query.

If you don't know how, you need to show some code (on how you open the query).

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: DBEdit - display setting of number (IBX)
« Reply #4 on: June 10, 2023, 02:30:36 pm »
This was the only thing, which worked just by clicking. I have little idea, what happens behind:
I have a DBEdit, a Query and an Update query.

"Above" it, there is a radio-box, where the user (=just me) can choose, which table he wants to edit.
Some table-choices work, others don't. I gave up, because it took too much time.
This choice was the only choice which worked fine, ;-)

The option-chosen we talk about leads to this OnClick-myTableEditbutton:

 
Code: Pascal  [Select][+][-]
  1.     If (RadioGroup_bearbeiten.ItemIndex = 5) then // tbwaren
  2.       begin                                
  3.          if RadioGroupEx_bearbeiteWare.ItemIndex > 0 then begin  // das sollte ohnehin nie sein können, doch zur Sicherheit
  4.             ware_:=RadioGroupEx_bearbeiteWare.Items.strings[RadioGroupEx_bearbeiteWare.ItemIndex];
  5.             s:=Frame_Waren.KuerzelInWare(ware_); // ware_ brauche ich für die Query unverändert
  6.             Panel_WareKopf.Caption:='Stammdaten für ' + s; // schreibt z.B. Stammdaten Weizen
  7.             s:='select * from tbkennzahlen where COMM = :ware_';
  8.             s:=s + ' order by COMM';
  9.             IBQuery_Ware.SQL.Text:=s;
  10.             IBQuery_Ware.ParamByName('ware_').AsString:=ware_;
  11.             IBQuery_Ware.Active:=true;
  12.          end;  end;

There are a lot of input fields and tables this form was inteded to change in my db.
But only one DBEdit, for this specific, it looks like this

Code: Pascal  [Select][+][-]
  1. object DBEdit_LockLimit: TDBEdit
  2.   Left = 24
  3.   Height = 25
  4.   Top = 24
  5.   Width = 73
  6.   DataField = 'DAILY_LIMIT'
  7.   DataSource = DataSource_Ware
  8.   AutoSize = False
  9.   MaxLength = 4
  10.   TabOrder = 0
  11. end
  12.  

I hope, this is understandable. If it would be too complex, I leave it like it is.

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: DBEdit - display setting of number (IBX)
« Reply #5 on: June 10, 2023, 10:52:14 pm »
I take it DAILY_LIMIT is a field of the table tbkennzahlen.

In that case, just below the IBQuery_Ware.Active:=true; you need to set the displayformat and editformat for that field.

So:
Code: Pascal  [Select][+][-]
  1. IBQuery_Ware.Active := true;
  2. TBCDField(IBQuery_Ware.FieldByName('DAILY_LIMIT')).DisplayFormat := '#.00;; '; // ;; for empty if value is 0
  3. TBCDField(IBQuery_Ware.FieldByName('DAILY_LIMIT')).EditFormat := '#.00';

If you have any other fields which need to be formatted you can add them there too.

I normally have a function for this, which looks at all the fields after opening a table and tries to deduce what the format should be.
Something like this:
Code: Pascal  [Select][+][-]
  1. procedure DefaultDisplayFormat(ib: TDataSet);
  2. var
  3.   I: integer;
  4.   Srt: string;
  5. begin
  6.   for I := 0 to ib.FieldCount - 1 do
  7.   begin
  8.     if ib.Fields[I].DataType in [ftFloat] then
  9.     begin
  10.       Srt := StringOfChar('0', TFloatField(ib.Fields[I]).Precision);
  11.       Srt := '#,##0.' + Srt + ';; ';
  12.       TFloatField(ib.Fields[I]).DisplayFormat := Srt;
  13.       Srt := StringReplace(Srt, ',', '', [rfReplaceAll]);
  14.       TFloatField(ib.Fields[I]).EditFormat := Srt;
  15.     end;
  16.     if ib.Fields[I].DataType in [ { ftFloat, } ftCurrency, ftBCD, ftFMTBCD] then
  17.     begin
  18.       Srt := StringOfChar('0', TBCDField(ib.Fields[I]).size);
  19.       Srt := '#,##0.' + Srt + ';; ';
  20.       TBCDField(ib.Fields[I]).DisplayFormat := Srt;
  21.       Srt := StringReplace(Srt, ',', '', [rfReplaceAll]);
  22.       TBCDField(ib.Fields[I]).EditFormat := Srt;
  23.     end;
  24.     if ib.Fields[I].DataType in [ftInteger, ftSmallInt, ftLargeInt] then
  25.     begin
  26.       Srt := StringOfChar('#', TIntegerField(ib.Fields[I]).size - 1);
  27.       Srt := Srt + '0;; ';
  28.       // Srt := '#,##0;;.';
  29.       TIntegerField(ib.Fields[I]).DisplayFormat := Srt;
  30.       Srt := StringReplace(Srt, ',', '', [rfReplaceAll]);
  31.       TIntegerField(ib.Fields[I]).EditFormat := Srt;
  32.     end;
  33.   end;
  34. end;

And in the code:
Code: Pascal  [Select][+][-]
  1. IBQuery_Ware.Active := true;
  2. DefaultDisplayFormat(IBQuery_Ware);
This will automatically set the displayformat AND editformat for all the Integer and float fields to the correct decimals.

But if this is too difficult you (or it doesn't work for you if the TField.Size is incorrect), you can just use the first option above.
« Last Edit: June 10, 2023, 10:54:58 pm by rvk »

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: DBEdit - display setting of number (IBX)
« Reply #6 on: June 13, 2023, 02:32:06 pm »
Thank you so much for your patience!

I will find the lines and save your code in my Code-library for the day, when I may work with several fields.
Such days occur often earlier, than we think.

 

TinyPortal © 2005-2018