Delete rows in table

sefi

New Member
Licensed User
Longtime User
Hi.
I want to search a table for a value and when the value is found, to delete the row .
I tried the code below , but I got index error
-------
f = table1.RowCount -1
for j = 0 to f
If table1.Cell("Car",j) = 5 Then
table1.RemoveRow(j)
f = f - 1
End If
next
-------
Is there a beter way to do it ?

Thanks
Sefi.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
If the database is large then you could use TableSort to first sort the rows and then do a more optimized search (binary search).
The error you are seeing is caused due to the fact that the limit in the For loop is only calculated at the beginning (to save the calculation each loop).
You can change your code to:
B4X:
j = 0
Do While j < Table1.RowCount
 If table1.Cell("Car",j) = 5 Then
   table1.RemoveRow(j) 'Do not advance j here.
 else
   j = j + 1
 end if
Loop
 
Top