In our last Adventure in Data Land we gathered INPUT from a data file, filled a multi-dimensioned array, sorted it using a bubble-sort, and then printed the array to a file for OUTPUT. This tutorial contains routines added to the original program for the purpose of adding records to, or editing existing records in the data file previously used.
Editing any data file to add or change records/fields is easily done using Notepad or Wordpad. Some users however, are not exceptionally capable when it comes to performing computer tasks, and we don't wish to have our data file corrupted, so we want to write code to automate the tasks of adding to, or editing, the data file in use.
Before we can add/edit information, we have to ask the user what action will be performed.
[edit] 'Choose what will be done
PRINT "Add a new record? (1)"
PRINT "Edit an existing record? (2)"
PRINT "I changed my mind, just show records. (3)"
INPUT "Please enter your choice: "; resp$
sel=INT(VAL(resp$)) 'Get a numerical value from resp$
IF sel <1 OR sel > 3 THEN
NOTICE "ERROR! ERROR!"+CHR$(13)+"Invalid selection!"+CHR$(13)+ _
"Please try again!" : GOTO [edit]
END IF
SELECT CASE sel
CASE 1 : GOTO [addNew] 'Add a new record
CASE 2 : GOTO [chgRec] 'Edit an existing record
CASE 3 : GOTO [begin] 'Exit the add/edit routine
END SELECT
WAIT
Depending on INPUT from the user, the SELECT CASE statement will branch to the appropriate block of code.
Adding to the data file is a single task performed by getting INPUT from the user and then opening the data file for APPEND, which adds a record to the end of the file while leaving the remainder of the file unchanged. To add a record, we need to get INPUT from the user and format the data to APPEND.
From previous use, we know our data file has many records consisting of four fields each. Writing a block of code to get INPUT from the user might look like this.
[addNew]
INPUT "Surname? "; field1$
INPUT "Christian name? "; field2$
INPUT "Age? "; field3$
INPUT "Telephone? "; field4$
newRec$=field1$+", "+field2$+", "+field3$+", "+field4$ '<--No separator!
OPEN "myTestData.txt" FOR APPEND AS #1
PRINT #1, newRec$ 'Adds the string to the end of the data file
CLOSE #1
GOTO [begin]
WAIT
The WAIT statement prevents us from unintentionally continuing to the next block of code. The information for APPEND to the data file is entered when the user responds to prompts for INPUT. The newRec$ must have field separators inserted into the string because, unlike the MAINWIN, we cannot specify print positions using the comma (,) but a data file must have field separators to allow program code to read individual fields. You could use +CHR$(44)+CHR$(32)+ to separate data fields, but I find it easier to use +", "+ which requires fewer key strokes. Be sure to include the space following the comma, allowing program code to count the fields in the record with the wordCount(a$) FUNCTION we created, but DON'T put the field separator at the end of the string! Without the space, the entire record will be counted as a single field.
After adding the record to the data file, GOTO [begin] returns to the start of the program and displays all data in the file, including the newly added record.
Editing an existing record requires several tasks. First we have to ask the user which record needs to be modified, then we must find that record in the existing array. A business might identify records in the data file using account numbers. We might consider identifying the record to be changed by entering the surname, but suppose we have three Smith's, or two Jone's? How do we tell the computer which record we want to edit, without entering last name, first name, etc?
I decided to use the telephone number as the search$, but your code might use zip code (if used in the data file), social security number, patient record number, or whatever the purpose of your data file, so long as the search$ will identify ONLY the record to be edited.
[chgRec]
PRINT "Select the record to edit by entering the individual's phone number."
PRINT "Use the scroll bar if the record to edit is not visible."
INPUT "Phone? "; search$
FOR i=1 to ln
IF search$=myContacts$(i,4) THEN 'Search the array until found
PRINT myContacts$(i,1);", "; myContacts$(i,2);" "; myContacts$(i,3);" "; _
myContacts$(i,4)
PRINT "Enter the correct information for ALL fields."
INPUT "Surname? "; resp1$
INPUT "Christian name? "; resp2$
INPUT "Age? "; resp3$
INPUT "Telephone? "; resp4$
' = = = = Put the changes into myContacts$() array
myContacts$(i,1)= resp1$ 'surname
myContacts$(i,2)= resp2$ 'christian name
myContacts$(i,3)= resp3$ 'age
myContacts$(i,4)= resp4$ 'telephone number
' = = = = Save the changes to the data file
OPEN "myTestData.txt" FOR OUTPUT AS #1 'CAUTION: This will overwrite the current file!
FOR k=1 TO ln 'Print all fields from myContacts$() array
PRINT #1, myContacts$(k,1);", "; 'The surname
PRINT #1, myContacts$(k,2);", "; 'The christian name
PRINT #1, myContacts$(k,3);", "; 'The age
PRINT #1, myContacts$(k,4) 'The phone number
NEXT k
CLOSE #1
flag=1 'Flag to let us know a record has been changed
EXIT FOR
END IF
NEXT i
PRINT
IF flag=1 THEN
GOTO [showNew] 'Display the new myContacts$() array
ELSE
PRINT "Search string not found."
flag=0
INPUT "Press ENTER to continue."; resp$
GOTO [edit]
END IF
WAIT
After the user enters the telephone number as search$, we use a FOR/NEXT loop to find the telephone number and print all data fields for this record. Then we prompt the user to enter new information for ALL fields, even if we only wish to change the age or phone number, of the surname if Barbara, Joyce, or Mindy got married. Once the user has completed ALL fields, we assign the input to the appropriate array elements, and then we PRINT the entire contents of the array to update the data file. When the data file has been updated, we branch to [showNew] to view the contents of the file.
If the search$ is not found by the time we have searched myContacts$() array, we show a message to that effect and send the user back to [edit] where another search$ can be entered, or the edit/change routine can be exited.
This BAS program uses the MAINWIN for all input/output, because the intent is to show blocks of code which may be used for your program. Not every coder will use the same techniques to accomplish a given task, and your code may be modified to suit your needs.
When you use a GUI, you have other choices for branching events in your program. You might display your records in a COMBOBOX showing last name, first name only, and allow the user to click on a contact to display the full information.
You might also use a RADIOBUTTON or CHECKBOX to branch to the appropriate add/edit routine. When using a GUI you will make frequent use of the "!contents? varName" statement, but the advantage is the user only has to update the field to be changed, not all fields for the appropriate record. A GUI not only makes your program look more professional and user friendly, it allows you to take shortcuts by using controls and click events to branch to other blocks of code.
On the other hand, using the MAINWIN allows you to test the logic and flow of your program and quickly make changes without concerning yourself with Window Width, Window Height, and syntax for your controls, etc. When your program does what you expect it to, then you can build one or more GUI's to make the program more attractive and user friendly.