Qbasicnews.com

Full Version: Deleting random file records
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have searched endlessly for a solution--I even went to Microsoft's Visual Basic page to search for an answer, and found one, although it doesn't seem to work.

What I am trying to do is when I have a random-access file, I would like to be able to delete certain records in it. Microsoft, on their Visual Basic page, suggested that I rewrite all the "good" records (the records I didn't want to delete) to a seperate file and ignore the one I want to delete, then rename the file.

I tried this, the way I thought it would work, and it doesn't seem to at all.

I posted my short program to test this, so that anyone can find errors in it. Look specifically at the "deleterecord" (yellow) section.

Thanks for any help Big Grin

Quote:menu:
CLS
PRINT "Select an option: "
PRINT "[1] Enter a New Record"
PRINT "[2] View a Record"
PRINT "[3] Delete a Record"
PRINT "[4] File Info"
PRINT "[5] View All of the Records"
PRINT "[6] End"
PRINT
INPUT "Enter your choice: ", choice
SELECT CASE choice
CASE 1
GOTO enterrecord
CASE 2
GOTO viewrecord
CASE 3
GOTO deleterecord
CASE 4
GOTO fileinfo
CASE 5
GOTO viewall
CASE 6
END
CASE ELSE
GOTO menu
END SELECT


TYPE newrecord
nme AS STRING * 50
lnme AS STRING * 50
END TYPE

DIM student AS newrecord
enterrecord:
CLOSE
OPEN "test.dat" FOR RANDOM AS #1 LEN = LEN(student)
INPUT "Enter the student number ", idnum
INPUT "Enter the first name: ", student.nme
INPUT "Enter the last name: ", student.lnme

PUT #1, idnum, student
GOTO menu

viewrecord:
CLS
CLOSE
OPEN "test.dat" FOR RANDOM AS #1 LEN = LEN(student)
INPUT "Enter the student ID number: ", idnum
GET #1, idnum, student
PRINT "First Name: ", student.nme
PRINT "Last Name: ", student.lnme
INPUT "Press <enter> to go to the menu.", go$
GOTO menu

deleterecord:
CLS
CLOSE
OPEN "test.dat" FOR RANDOM AS #1 LEN = LEN(student)
INPUT "Enter the ID number: ", idnum2
OPEN "test2.dat" FOR RANDOM AS #2 LEN = LEN(student)

FOR x = 1 TO LOF(1) / LEN(student)
GET #1, x, student
IF x = idnum2 THEN GOTO nextx
PUT #2, x, student
nextx:
NEXT x
CLOSE
KILL "test.dat"
SHELL "rename test2.dat test.dat"

GOTO menu


fileinfo:
CLS
CLOSE
OPEN "test.dat " FOR RANDOM AS #1 LEN = LEN(student)
PRINT LOF(1); "bytes"
PRINT "No. of records: "; LOF(1) / LEN(student)
SLEEP 5
GOTO menu

viewall:
CLOSE
OPEN "test.dat" FOR RANDOM AS #1 LEN = LEN(student)
FOR x = 1 TO LOF(1) / LEN(student)
GET #1, x, student
PRINT "No: ", x
PRINT "First Name: ", student.nme
PRINT "Last Name: ", student.lnme
NEXT x
SLEEP 5
GOTO menu
Actually the Database programs just mark the delete records with a flag and ignore them when listing or searching. Only when a "Compact database " maintenance operation the file is copied with the deleted records omitted.

Your code is copying the good records and leaving a blank in the place of the deleted record. To omit the record simply erase the x in
Code:
PUT #2,X,STUDENT

and leave it as
Code:
PUT #2,,STUDENT

this way the record number will be increased by 1 with each PUT, and the place for the deleted record will not ber left.
Thanks!

That's exactly what I needed. :lol: