Please help: TDbf.RecNo do not working properly lazarus / 16-Databases ---------------------------------------------------------------------- Handoko · 2009-03-06 06:11 I'm using: Lazarus 0.9.26 Beta i386-win32 SVN 16885 FPC 2.2.2 DBFLaz 0.1.1 Hi, I found a bug in unit dbf (DBFLaz package). If you insert some record, delete it, and insert some new record again. The data you read will not be correct. I have tried it several times using different data, I'm sure it always happen. Packing the table will solve the problem. And if you create a new table, insert some record and read it (without any deletion previously), you will get the correct data. Strangely, TDBGrid will show the record correctly. I also try using CDBF Explorer (a freeware dbf utility), the record show correctly. So I think the problem only happend if I use coding to the manipulate the data. What I suspect is RecNo := xxx do not working properly. Here, I explain it simply: - A bug in TDbf (unit dbf, DBFLaz package) - Record read incorrectly if there is any deleted record previously - Packing the table can solve the problem - Problem happen if you use coding to manipulate the record - Problem will not happen if you use a TDBGrid to show the data - I suspect TDbf.RecNo := xxx do not working properly Of course, I can pack the table each time before I use coding to manipulate the data. But you know it is not a good solution. I've report the bug at Lazarus Bug Tracker. Anybody here please help me. If you experience same problem, post your comment at the Bug Tracker forum ID no. 13285. Here is a sample code: (uses unit dbf and db) procedure TForm1.Button1Click(Sender: TObject); var   DbfTest: TDbf;   Sum: Integer;   Z: Integer; begin   DbfTest := TDbf.Create(nil);   with DbfTest do begin     FilePathFull := 'c:\';     TableName := 'Test.dbf';     FieldDefs.Add('QTY', ftInteger);     CreateTable;     Open;     // Insert data, then delete it     Insert; FieldByName('QTY').AsInteger := 20; Post;     Delete;     // Insert 20 and 15, so the summary should be 35     Insert; FieldByName('QTY').AsInteger := 20; Post;     Insert; FieldByName('QTY').AsInteger := 15; Post;     // Start calculating     Sum := 0;     for Z := 1 to ExactRecordCount do begin       RecNo := Z;       Sum := Sum + FieldByName('QTY').AsInteger;       end;     // But the result do not as expected     MessageDlg('Result', IntToStr(Sum), mtInformation, [mbOK], 0);     Close;     Free;     end; end; ---------------------------------------------------------------------- DougNettleton · 2009-03-06 16:22 I'm not sure I would call it a bug. You've got 3 records on your file.  The first one is deleted.  Your code says basically: goto record 1, add qty to summary goto record 2, add qty to summary and because you've deleted record 1 you expect it to read records 2 and 3.  Let's say you had 10 records and you deleted 2,4,6,8, and 10, would expect your code to read records 1,3,5,7, and 9?  And what would you expect if "ShowDeleted" was set to true? try:     Sum := 0;     Dbf1.First;     while not Dbf1.EOF do     begin       Sum := Sum + FieldByName('QTY').AsInteger;       Dbf1.Next;     end; HTH, Doug