Recent

Author Topic: [SOLVED] dbf and deleting records in a loop  (Read 4686 times)

timcs

  • Full Member
  • ***
  • Posts: 213
[SOLVED] dbf and deleting records in a loop
« on: January 03, 2013, 10:47:32 pm »
Below is my code where I am trying to delete records from a dbf where the date in each record is not today please can some point out where I am going wrong as when I run this, it only appears to be deleting some of the records and each time I click the button that runs this, it does some more BUT even deletes todays record!

Code: [Select]
if dbf1.active = true then
      begin
         // dbf1.Filtered:=true;
           dbf1.IndexName:='';
           prev := 1;
           dbf1.Filtered:=true;

           dbf1.DisableControls;
           if dbf1.ExactRecordCount>0 then
           begin

               dbf1.Filtered:=false;
               dbf1.Active:=false;
               dbf1.Active:=true;
               dbf1.First;
               while not dbf1.EOF do
               begin
                  if DateOf(dbf1.FieldByName('dateon').AsDateTime)<DateOf(now) then
                     dbf1.Delete;

                  dbf1.Next;

                end;

             dbf1.TryExclusive;

             dbf1.PackTable;
             dbf1.EndExclusive;

           dbf1.Filtered:=false;

           dbf1.Active:=false;
           dbf1.Active:=true;
           dbf1.EnableControls;
           dbf1.first;


           prev := 0;
         end;                         

the variable prev is set to trigger the required filter in the OnFilterRecord procedure :

Code: [Select]
if prev = 1 then
   begin
   accept := (DateOf(Dataset.FieldByName('dateon').AsDateTime)<DateOf(now));

   end;

Any Ideas please, I thought this would work but it appears not to

Thanks

TimCS
« Last Edit: January 04, 2013, 06:15:46 pm by timcs »

ttomas

  • Full Member
  • ***
  • Posts: 245
Re: dbf and deleting records in a loop
« Reply #1 on: January 04, 2013, 01:16:40 am »
Code: [Select]
               
if DateOf(dbf1.FieldByName('dateon').AsDateTime)<DateOf(now) then
  dbf1.Delete
else
  dbf1.Next;

When you delete record your cursor is moved to next record and with next dbf1.Next you skip next record

timcs

  • Full Member
  • ***
  • Posts: 213
Re: dbf and deleting records in a loop
« Reply #2 on: January 04, 2013, 01:25:21 am »
Code: [Select]
               
if DateOf(dbf1.FieldByName('dateon').AsDateTime)<DateOf(now) then
  dbf1.Delete
else
  dbf1.Next;

When you delete record your cursor is moved to next record and with next dbf1.Next you skip next record

Hi ttomas thanks for your reply such a simple piece of logic sometimes I think I look at the code too long and too hard not seeing the obvious !!!. I think I just had it in my head that it would move next anyway but if that record match the if condition it would delete it and then move to the next record to see if that matched etc..

Still if I move to the next record at the end of the loop why would it skip a record before hitting the if condition ? the move next is separate to the if condition not part of it. Sorry for the stupid question, I think I have been doing too many other programming languages at the same time  :o

Thanks

TimCS

ttomas

  • Full Member
  • ***
  • Posts: 245
Re: dbf and deleting records in a loop
« Reply #3 on: January 04, 2013, 01:38:21 am »
Still if I move to the next record at the end of the loop why would it skip a record before hitting the if condition ? the move next is separate to the if condition not part of it.
dbf1.Delete - delete record and move to next record.
With your dbf1.Next your cursor is moved again and skip 1 record (record after deleted record).
Put DBGrid and look for your cursor when you delete record.

timcs

  • Full Member
  • ***
  • Posts: 213
Re: dbf and deleting records in a loop
« Reply #4 on: January 04, 2013, 10:44:38 am »
Still if I move to the next record at the end of the loop why would it skip a record before hitting the if condition ? the move next is separate to the if condition not part of it.
dbf1.Delete - delete record and move to next record.
With your dbf1.Next your cursor is moved again and skip 1 record (record after deleted record).
Put DBGrid and look for your cursor when you delete record.

That now makes sense I did not release the delete did that , thanks ttomas

TimCS

 

TinyPortal © 2005-2018