Subject Re: Delete (all) in a form
From Mervyn Bick <invalid@invalid.invald>
Date Mon, 13 Aug 2018 16:53:09 +0200
Newsgroups dbase.getting-started

On 2018-08-13 2:39 PM, Charlie wrote:
> Hi Akshat...
>
> I think either the form or the table are haunted!!!
>
> I changed to a different form of delete in this:
>
> function PUSHBUTTON1_onClick()
>         p=form.pictemp11.rowset
>     //form.train1.emptyTable( "pictemp1" )
>         cnt=p.count()
>         msgbox ( ""+ cnt )
>         p.first()
>         do while cnt > 0
>            p.delete()
>                 cnt = cnt-1
>                 msgbox( ""+CNT )
>                 p.next()
>         enddo
>
> I put message boxes so that I could see what was happening in slow motion.  The cnt was correct at the start at 4, then it deleted the first row.  Then it went to 3, but then it deleted the third row instead of the second row.  (I have two fields in the table.  One is a number field indicating what the number of the row is.)  OK so now still alive in the table is the second and forth row; and then it gives me the end of set error.

There's nasty little "gotcha" hidden in your code.  When you delete a
record the row pointer is automatically placed on the next record.  When
p.next() is executed it moves the row pointer to the next record which
saves the previous record from the executioner. :-)

Try the following.  It WILL delete all the records. :-)

    function PUSHBUTTON1_onClick()
       p=form.pictemp11.rowset
       p.first()
       do while not p.endofset
          p.delete()
       enddo
       return

Be aware that deleting records like this doesn't physically remove the
records.  They are marked as deleted so dBASE doesn't display them but
they are still in the file.  If you delete a lot of records frequently
the file can grow disproportionately large.  You should periodically
PACK the table.

oDatabase.packTable() will only work if nothing else is accessing the
table but it does mean that you start with an empty table and no "bloat".


Mervyn.