Simple Contact Database

General FreeBASIC programming questions.
Post Reply
RNBW
Posts: 264
Joined: Apr 11, 2015 11:06
Location: UK

Simple Contact Database

Post by RNBW »

I picked up an old QBasic database stored on my computer and converted it to work in FreeBasic. I then expanded it a bit because it only covered name, phone and email address. I don't know its origin but it does work quite well, if a little basic. It could be of use to a beginner in basic who wants to produce some useful code.

Code: Select all

    '=============================
    '  SIMPLE DATABASE PROGRAM
    '  Converted from QBasic to FreeBasic
    '=============================
   
      TYPE RECORD
          NAME AS STRING * 30
          phone AS STRING * 20
          mobile AS STRING * 20
          email AS STRING * 30
          address AS STRING * 60
          city AS STRING * 60
          county AS STRING * 20
          zip AS STRING * 10
      END TYPE
   
      DIM SHARED contact(100) AS RECORD
      DIM SHARED temp(1) AS RECORD
      DIM SHARED temp2(1) AS RECORD
      DIM SHARED AS INTEGER counter, j, remRec, loops, t, u, g, i, p
      DIM SHARED AS STRING filename
   
      '------------------------------
      '    SUBS
      '------------------------------
      
     'SUB load
      SUB LOAD
      filename = "Phonenos.idx"
      OPEN filename FOR INPUT AS #1
      INPUT #1, counter
      CLOSE #1
      filename = "Phonenos.dat"
      OPEN filename FOR INPUT AS #2
      FOR p = 1 TO counter
          INPUT #2, contact(p).name
          INPUT #2, contact(p).phone
          INPUT #2, contact(p).mobile
          INPUT #2, contact(p).email
          INPUT #2, contact(p).address
          INPUT #2, contact(p).city
          INPUT #2, contact(p).county
          INPUT #2, contact(p).zip
      NEXT p
      CLOSE #2
      END SUB
      
      
      'SUB create
      SUB CREATE
      counter = counter + 1
      INPUT "Name: ", contact(counter).name
      INPUT "Phone number: ", contact(counter).phone
      INPUT "Mobile number: ", contact(counter).mobile
      INPUT "Email address: ", contact(counter).email   
      PRINT "Address excluding City, County and Zip:"
      INPUT "Address: ", contact(counter).address
      INPUT "City: ", contact(counter).city
      INPUT "County/State: ", contact(counter).county
      INPUT "Zip: ", contact(counter).zip
      END SUB
   
      'SUB save
      SUB SAVE
      filename = "Phonenos.idx"
      OPEN filename FOR OUTPUT AS #1
      PRINT #1, counter
      CLOSE #1
      filename = "Phonenos.dat"
      OPEN filename FOR OUTPUT AS #2
      FOR j = 1 TO counter
          PRINT #2, contact(j).name
          PRINT #2, contact(j).phone
          PRINT #2, contact(j).mobile
          PRINT #2, contact(j).email
          PRINT #2, contact(j).address
          PRINT #2, contact(j).city
          PRINT #2, contact(j).county
          PRINT #2, contact(j).zip
      NEXT j
      CLOSE #2
      PRINT "Data saved."
      END SUB
   
      'SUB del
      SUB DEL
      INPUT "Enter the number of a record you wish to delete ", remRec
      FOR loops = remRec TO counter - 1
          contact(loops).name = contact(loops + 1).name
          contact(loops).phone = contact(loops + 1).phone
          contact(loops).mobile = contact(loops + 1).mobile
          contact(loops).email = contact(loops + 1).email
          contact(loops).address = contact(loops + 1).address
          contact(loops).city = contact(loops + 1).city
          contact(loops).county = contact(loops + 1).county
          contact(loops).zip = contact(loops + 1).zip
      NEXT loops
      counter = counter - 1
      END SUB
   
      'SUB sort
      SUB SORT
      FOR t = 1 TO counter - 1
          FOR u = t + 1 TO counter
              IF contact(t).name > contact(u).name THEN
                  temp(1) = contact(t)
                  contact(t) = contact(u)
                  contact(u) = temp(1)
              END IF
          NEXT u
      NEXT t
      PRINT "Sorted."
      END SUB
   
      'SUB listrecords
      SUB LISTRECORDS
      FOR g = 1 TO counter
          PRINT
          PRINT "Record number "; g
          PRINT "Name: "; contact(g).name
          PRINT "Phone: "; contact(g).phone
          PRINT "Mobile: "; contact(g).mobile
          PRINT "Email: "; contact(g).email
          PRINT "Address:"; contact(g).address
          PRINT "City: "; contact(g).city
          PRINT "County: "; contact(g).county
          PRINT "Zip: "; contact(g).zip
          PRINT
      NEXT g
      END SUB
   
      'SUB search
      SUB SEARCH
      INPUT "Enter the name of a record to look for ", temp2(1).name
      FOR i = 1 TO counter
          IF contact(i).name = temp2(1).name THEN
              PRINT
              PRINT contact(i).name
              PRINT contact(i).phone
              PRINT contact(i).mobile
              PRINT contact(i).email
              PRINT contact(i).address
              PRINT contact(i).city
              PRINT contact(i).county
              PRINT contact(i).zip
              PRINT
          END IF
      NEXT i
      END SUB
   
   
   
   '==================================
   '==================================
   '   MAIN PROGRAM
   '==================================
   '==================================
   
      counter = 0
      DIM SHARED AS INTEGER choice
      WHILE choice <> 8
          PRINT
          PRINT "1. Load the data file."
          PRINT "2. Create a record."
          PRINT "3  Save records to file."
          PRINT "4. Delete a record."
          PRINT "5. Sort records."
          PRINT "6. List records."
          PRINT "7. Search for a record."
          PRINT "8. Exit."
          INPUT "Choose... ", choice
          CLS
          PRINT
    
           SELECT CASE choice
               CASE 1
                  LOAD
               CASE 2
                  CREATE
               CASE 3
                  SAVE
               CASE 4 
                  DEL
               CASE 5
                  SORT
               CASE 6
                  LISTRECORDS
               CASE 7
                  SEARCH
               CASE 8
                  END
           END SELECT        
      WEND
      
      END 
Post Reply