Recent

Author Topic: [SOLVED] TDBf Primary Fields  (Read 1433 times)

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 397
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
[SOLVED] TDBf Primary Fields
« on: February 05, 2024, 09:50:12 pm »
Has anyone questioned why when adding the first record to a Dbf table containing a AutoInc-primary field, that field's value is assigned 0 instead of 1?  This was a question I was asked by one of my users who is testing this new app? I'm relatively new to programming. So, I went into the system and in every table with a primary AutoInc primary field I added a new record first, copied it to a new record and then deleted the 0 record.  Now the 3 people testing this new app are happier because there are no 0 records  ::)!
« Last Edit: February 14, 2024, 04:31:02 pm by 1HuntnMan »

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Re: TDBf Primary Fields
« Reply #1 on: February 06, 2024, 08:33:23 am »
What's wrong with 0 for a (Primary Key?) Auto-Inc-Field?
It's a value you (and anyone else) shouldn't be interested in at all
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

korba812

  • Sr. Member
  • ****
  • Posts: 482
Re: TDBf Primary Fields
« Reply #2 on: February 06, 2024, 12:41:26 pm »
What's wrong with 0 for a (Primary Key?) Auto-Inc-Field?
It's a value you (and anyone else) shouldn't be interested in at all
In the case of DBF, this is important because standard numeric fields do not know NULL value and the zero (0) value is used instead. Most Auto-Inc implementations in DBF number from 1.

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Re: TDBf Primary Fields
« Reply #3 on: February 06, 2024, 03:31:30 pm »
In the case of DBF, this is important because standard numeric fields do not know NULL value and the zero (0) value is used instead. Most Auto-Inc implementations in DBF number from 1.
OK. Accepted.
Didn't know that, since the last time i was in "contact" with dBase was 1996 in economic school

EDIT: Found something in "...\source\packages\fcl-db\src\dbase\dbf_dbffile.pas"
Line 854 (FPC3.2.2 - 32Bit)
Code: Pascal  [Select][+][-]
  1.  // Update our field list
  2.       with FFieldDefs.AddFieldDef do
  3.       begin
  4.         Assign(lFieldDef);
  5.         Offset := lFieldOffset;
  6.         AutoInc := 0;
  7.       end;
There.
AutoInc:=0;

and looking further through the file, any mention of "AutoInc" that has a direct assignment, assigns "0", even "local" variables containing "AutoInc" in its name

https://www.freepascal.org/daily/packages/fcl-db/dbf_fields/tdbffielddef.html
As far as i understand it: If you create everything from FPC-Code, you should be able to set AutoInc to whatever Startvalue you want (Don't forget AutoIncStep) when you create the FieldDefs.
« Last Edit: February 06, 2024, 04:36:37 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 397
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: TDBf Primary Fields
« Reply #4 on: February 11, 2024, 10:34:44 pm »
I totally agree and understand that a Primary/AutoInc field starts with 0. But, the users could give a flip. They just don't like a 0 Appointment ID. They want the Appointment No. to be a 1 and increment from there. I guess I should have given them a true Appointment No./ID field instead of using the Primary key. Would be easy to change... Thanks

 

TinyPortal © 2005-2018