Quote:could you please post an example?
The below was coded from memory straight into these forums, I haven't tested it, and it hasn't been written to be run if loaded in QB you'll need to modify it so it doesn't just run through each of the stages
[syntax="QBASIC"]TYPE contact
FullName AS STRING * 20
Number AS STRING * 10
END TYPE
DIM person AS contact
filename$="fonebook.txt" 'Complies with DOS's 8.3 file names
'*** test to see if file exist's ***
OPEN filename$ FOR BINARY AS #1
IF LOF(1) = 0 THEN
CLOSE #1
KILL filename$
' Insert code for starting a new phone book (you could just go on to the write to file section
ELSE
CLOSE #1
' Insert code to continue adding to file.
END IF
'*** To write to the file ***
OPEN filename$ FOR RANDOM AS #1 LEN=LEN(person)
DO
numContacts = LOF(1) / LEN(person)
INPUT "Name :", person.fullname
INPUT "Number :", person.number
PUT #1, numContacts+1, person
PRINT "Press Escape to quit, any other key to continue"
DO
key$=INKEY$
LOOP UNTIL key$ <> ""
LOOP UNTIL key$ = CHR$(27)
CLOSE #1
'*** To read from the file ***
OPEN filename$ FOR RANDOM AS #1 LEN=LEN(person)
numContacts = LOF(1) / LEN(person)
FOR i = 1 TO numContacts
GET #1, , person
PRINT "Name :", person.fullname
PRINT "Number :", person.number
NEXT i
CLOSE #1
'*** To find a contact ***
OPEN filename$ FOR RANDOM AS #1 LEN=LEN(person)
'So the search string and record string are the same length
DIM searchName AS STRING * 20
INPUT "Please enter the name to search for", searchName
numContacts = LOF(1) / LEN(person)
FOR i = 1 TO numContacts
GET #1, , person
IF person.fullname = searchName THEN
PRINT "Name :", person.fullname
PRINT "Number :", person.number
EXIT FOR 'No need to search the rest of the file (you could ommit this line so the search brings up all instances not just the first)
END IF
NEXT i
CLOSE #1[/syntax]
If you use sequential access you need to use INPUT to read from the file into QB. OUTPUT to create a new file. APPEND to add data onto the end of a file. If you use OUTPUT on a file that already exists you'll overwrite the data already stored in the file. Using sequential access you would also need to recreate the whole file if you wanted to change some information inside it.
Random access is better for file's whos information changes. Sequential is only really usefull for logs and that type of write once data.
Using a combination of find contact and write to file you can edit a contact's details.
Also I don't like you idea of using QB's ON ERROR, hence I've provided you with a better way of checking if a file exists. This way if there is a problem with your code QB will flash up a message that means somehting and point you (hopfully) to the line that caused the problem.