I'm trying to delete a single record from a rnd file using a delete button. On click the code below the problem seems to be with the keep statement. I keep getting a compile error:invalid qualifier on selectedbook even though the value matches the position of the record in the file. any ideas anyone ? :?:


Dim newrecordcount As Integer 'New recordcount for updated records'
Dim Book As BookType 'Book as single record'
Dim BookArray() As BookType 'Book array = dynamic'
ReDim BookArray(1 To recordcount) 'Redefine array to recordcount'
Dim N As Integer 'Define loop variable'

selectedbook.keep = False 'flag book record to remove'

Open filename For Random As #1 Len = Len(Book) 'Open file'

For N = 1 To recordcount
Get #1, N, Book
If selectedbook = True Then 'populate array'
newrecordcount = newrecordcount + 1 'with records but flag'
BookArray(newrecordcount) = Book 'selectedbook as false'
End If
Next N
Close #1 'close file

Kill filename 'kill file'

The file can then be rewritten to the same file path.

The easiest way to 'delete' a record in a random file is to write (put)
for RN&=Record&+1 to NR& (number of records)
Get #1,RN&,UDT
PUT #1,RN&-1,UDT
next

then have some way of controlling what the total records are (NR&) and reduce it by 1, such as a separate file containing the number of records or
as another UDT of the same length in Record 1 (so that the actual data
records exist in records 2 to NR&+1).

I do this as well as insert records in my Client Writeup for CPAs program.

If you clear the entire file, then you can rewrite records 1 to Record&-1
first.

UDT of course, stands for a User-defined Type.
in QB if you have a large file and are putting the entire UDT records in an array, you might run into memory problem. With PowerBasic for windows, you shouldn't have that problem.

I was making a big mistake here by trying to make 'selectedbook' false when it contained a true variable, which is not possible in VB. The problem has been sorted now by doing as you mentioned, deleting the selected variable stringtype and rewriting the file. Thanks