Recent

Author Topic: Indexes are off...  (Read 407 times)

OC DelGuy

  • Full Member
  • ***
  • Posts: 222
Indexes are off...
« on: November 26, 2025, 12:33:01 pm »
To all those in the US, Happy Thanksgiving!
Hello Everyone.
Thanks in advance for any help you can provide! :) :D

When I click on btnNewPerson, the EdtName, DteDOB and MemDescription are not being written to the Record.  My index is -1.  It's supposed to be 0 and then +1 makes the index 1.  Where am I going wrong?

The code in in a txt file because I got an Error while trying to post.  I know, Benny likes to see All the code...   Hi Benny!  ;) ;D
Free Pascal Lazarus Version #: 4.6
Date: 16 MAR 2026
FPC Version: 3.2.2
Revision: Lazarus_4_6
x86_64-win64-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 19620
  • Glad to be alive.
Re: Indexes are off...
« Reply #1 on: November 26, 2025, 12:41:17 pm »
call TDataset.first.
-1 means that an index is not yet known.
Calling first goes to index 0.
« Last Edit: November 26, 2025, 12:52:20 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

paweld

  • Hero Member
  • *****
  • Posts: 1700
Re: Indexes are off...
« Reply #2 on: November 26, 2025, 02:13:41 pm »
Hi,
This is because you update the fields before adding the record. And if you have an empty record table, the update will not be performed because CurrentPersonIndex is -1.
The easiest way is to modify the procedure as follows:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.BtnNewPersonClick(Sender: TObject);
  2. var
  3.   NewIndex: Integer;
  4. begin
  5.   UpdateCurrentRecord;           // Save current components to list
  6.   NewIndex := Length(PersonDataList);    // Get size of current list for new record index
  7.   ShowMessage('Length(PersonDataList):  ' + IntToStr(Length(PersonDataList)) + '     NewIndex:  ' + IntToStr(NewIndex));
  8.   SetLength(PersonDataList, NewIndex + 1);                                                // Resize array for new record
  9.   FillChar(PersonDataList[NewIndex], SizeOf(TPersonRecord), 0);                                 // Initialize new record
  10.   SetLength(PersonDataList[NewIndex].Events, 0);                                       // Initialize inner array
  11.   PersonDataList[NewIndex].FullName := 'New Person ' + IntToStr(NewIndex + 1);               // Set temp name in ListBox
  12.   if CurrentPersonIndex < 0 then                                               //if first record
  13.     UpdateCurrentRecord;                                                       //set names and dates
  14.   LoadListBox;  LbNames.ItemIndex := NewIndex;                                // Update ListBox with new name and select
  15.   CurrentPersonIndex := NewIndex;                                                     // Set new index as current record
  16.   DisplayCurrentRecord;                                                            // Display empty fields of new record
  17.   DteDOB.SetFocus;
  18. end;  
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018