home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / basic / library / qb_pds / database / jdb10 / jdb.bas < prev    next >
Encoding:
BASIC Source File  |  1993-12-19  |  12.7 KB  |  455 lines

  1. 'PROGRAM NAME: JDB.BAS, Joe's DataBase
  2. 'WRITTEN BY  : Joe Caverly
  3. '              J&T Data Pros
  4. '              St. Thomas, Ontario
  5. 'VERSION     : 1.0, 93/12/19
  6. 'NOTES       : This is a QBASIC Program which I am developing. Your input
  7. '              and questions are requested. This software and accompanying
  8. '              materials are distributed "as is" without charge and without
  9. '              warranty, express, implied or statutory, including but not
  10. '              limited to any implied warranties of merchantability and
  11. '              fitness for a particular purpose.  In no event shall anyone
  12. '              involved with the creation and production of this product be
  13. '              liable for indirect, special, or consequential damages,
  14. '              arising out of any use thereof or breach of any warranty.
  15. '
  16. '              You may not upload modified versions of the code to any
  17. '              electronic BBS. All changes must be sent to me for inclusion
  18. '              in the program, at which time you will be given credit in the
  19. '              program for your contribution.
  20. '
  21. '              You may upload this program, as is and unmodified, to any
  22. '              electronic BBS. In fact, I request that you do so, in order
  23. '              for me to receive feedback from all those interested. I can be
  24. '              reached at the Home Office Business Exchange BBS 519-633-6574
  25. '              or Internet 72500.2405@compuserve.com.
  26.  
  27. CONST FALSE = 0
  28. CONST TRUE = NOT FALSE
  29.   
  30. ' Key code numbers
  31. CONST BACKSPACE = 8
  32. CONST CTRLLEFTARROW = 29440
  33. CONST CTRLRIGHTARROW = 29696
  34. CONST CTRLY = 25
  35. CONST CTRLQ = 17
  36. CONST DELETE = 21248
  37. CONST DOWNARROW = 20480
  38. CONST ENDKEY = 20224
  39. CONST ENTER = 13
  40. CONST ESCAPE = 27
  41. CONST HOME = 18176
  42. CONST INSERTKEY = 20992
  43. CONST LEFTARROW = 19200
  44. CONST RIGHTARROW = 19712
  45. CONST TABKEY = 9
  46. CONST UPARROW = 18432
  47.   
  48. ' Functions
  49. DECLARE FUNCTION KeyCode% ()
  50. DECLARE FUNCTION InKeyCode% ()
  51.  
  52. ' Subprograms
  53. DECLARE SUB Editline (a$, exitcode%)
  54.  
  55. ' If there is an error, process it
  56. ON ERROR GOTO ErrHandler
  57.  
  58. ' A variable indicating the maximum number of records in the file
  59. LET MaxRecs = 200
  60. LET Choice = 0
  61.  
  62. ' Define the structure of the data record
  63. TYPE DataDef
  64.   Status AS STRING * 1
  65.   LastName AS STRING * 15
  66.   FirstName AS STRING * 10
  67.   City AS STRING * 15
  68.   Province AS STRING * 2
  69.   PostalCode AS STRING * 6
  70.   Telephone AS STRING * 10
  71. END TYPE
  72.  
  73. ' Indicate how many records can be in the file
  74. DIM Record(1 TO MaxRecs) AS DataDef
  75.  
  76. ' Determine the File Size
  77. LET FileSize = MaxRecs * LEN(Record(1))
  78.  
  79. ' Load The File Into The Memory Array
  80. GOSUB LoadFile
  81.  
  82. ' Display Main Menu
  83. DO WHILE Choice <> 5
  84.   GOSUB MainMenu
  85. LOOP
  86.  
  87. ' End of Program
  88. END
  89.  
  90. ' Add A Record To The File
  91. AddARecord:
  92.   SR$ = "ADDARECORD"                    'Indicate the current subroutine
  93.   LastName$ = STRING$(LEN(Record(1).LastName), " ")
  94.   FirstName$ = STRING$(LEN(Record(1).FirstName), " ")
  95.   City$ = STRING$(LEN(Record(1).City), " ")
  96.   Province$ = STRING$(LEN(Record(1).Province), " ")
  97.   PostalCode$ = STRING$(LEN(Record(1).PostalCode$), " ")
  98.   Telephone$ = STRING$(LEN(Record(1).Telephone), " ")
  99.   CLS
  100.   LOCATE 1, 1: PRINT "Last Name   "
  101.   LOCATE 2, 1: PRINT "First Name  "
  102.   LOCATE 3, 1: PRINT "City        "
  103.   LOCATE 4, 1: PRINT "Province    "
  104.   LOCATE 5, 1: PRINT "Postal Code "
  105.   LOCATE 6, 1: PRINT "Telephone   "
  106.   COLOR 14, 1
  107.   LOCATE 1, 14: Editline LastName$, exitcode%
  108.   LOCATE 2, 14: Editline FirstName$, exitcode%
  109.   LOCATE 3, 14: Editline City$, exitcode%
  110.   LOCATE 4, 14: Editline Province$, exitcode%
  111.   LOCATE 5, 14: Editline PostalCode$, exitcode%
  112.   LOCATE 6, 14: Editline Telephone$, exitcode%
  113.   COLOR 7, 0
  114.   IF LastName$ = STRING$(LEN(Record(1).LastName), " ") THEN
  115.     'Next Sentence
  116.   ELSE
  117.     GOSUB SaveARecord
  118.   END IF
  119. RETURN
  120.  
  121. DeleteARecord:
  122.   SR$ = "DELETEARECORD"
  123.   INPUT "Record To Delete:"; RecToDel
  124.   IF RecToDel > 0 AND RecToDel < MaxRecs THEN
  125.     PRINT Record(RecToDel).LastName
  126.     PRINT Record(RecToDel).FirstName
  127.     PRINT Record(RecToDel).City
  128.     PRINT Record(RecToDel).Province
  129.     PRINT Record(RecToDel).PostalCode
  130.     PRINT Record(RecToDel).Telephone
  131.     PRINT
  132.     INPUT "Delete This Record (Y/N)"; YorN$
  133.     IF YorN$ = "Y" OR YorN$ = "y" THEN
  134.       Record(RecToDel).Status = " "
  135.       GOSUB SaveFile
  136.     END IF
  137.   END IF
  138. RETURN
  139.  
  140. DisplayAll:
  141.   SR$ = "DISPLAYALL"
  142.   FOR RecCtr = 1 TO MaxRecs
  143.     IF Record(RecCtr).Status = "A" THEN
  144.       PRINT RecCtr;
  145.       PRINT Record(RecCtr).LastName;
  146.       PRINT Record(RecCtr).FirstName
  147.     END IF
  148.   NEXT RecCtr
  149.   PRINT
  150.   PRINT "Press the <Space Bar> to continue..."
  151.   WHILE INKEY$ = "": WEND
  152. RETURN
  153.  
  154. MainMenu:
  155.   SR$ = "MAINMENU"
  156.   CLS
  157.   PRINT "1) Add A Record"
  158.   PRINT "2) Change A Record"
  159.   PRINT "3) Delete A Record"
  160.   PRINT "4) Display All"
  161.   PRINT "5) Quit"
  162.   INPUT Choice
  163.  
  164.   SELECT CASE Choice
  165.     CASE 1
  166.       GOSUB AddARecord
  167.     CASE 3
  168.       GOSUB DeleteARecord
  169.     CASE 4
  170.       GOSUB DisplayAll
  171.     CASE otherwise
  172.       PRINT CHR$(7)
  173.   END SELECT
  174. RETURN
  175.  
  176. SaveARecord:
  177.   SR$ = "SAVEARECORD"
  178.   FoundEmpty$ = "N"
  179.   FOR RecCtr = 1 TO MaxRecs
  180.     IF Record(RecCtr).Status <> "A" AND FoundEmpty$ = "N" THEN
  181.       LET Record(RecCtr).Status = "A"
  182.       LET Record(RecCtr).LastName = LastName$
  183.       LET Record(RecCtr).FirstName = FirstName$
  184.       LET Record(RecCtr).City = City$
  185.       LET Record(RecCtr).Province = Province$
  186.       LET Record(RecCtr).PostalCode = PostalCode$
  187.       LET Record(RecCtr).Telephone = Telephone$
  188.       GOSUB SaveFile
  189.       FoundEmpty$ = "Y"
  190.     END IF
  191.   NEXT RecCtr
  192.   IF FoundEmpty$ = "N" THEN
  193.     PRINT "File is Full"
  194.   END IF
  195. RETURN
  196.  
  197. SaveFile:
  198.   SR$ = "SAVEFILE"
  199.   DEF SEG = VARSEG(Record(1))
  200.   BSAVE "JDB.DAT", 0, FileSize
  201. RETURN
  202.  
  203. LoadFile:
  204.   SR$ = "LOADFILE"
  205.   DEF SEG = VARSEG(Record(1))
  206.   BLOAD "JDB.DAT", 0
  207. RETURN
  208.  
  209. ErrHandler:
  210.   SELECT CASE ERR
  211.     CASE 53
  212.       IF SR$ = "LOADFILE" THEN
  213.         GOSUB SaveFile
  214.         RESUME
  215.       END IF
  216.     CASE ELSE
  217.       PRINT ERR
  218.       PRINT SR$
  219.       END
  220.   END SELECT
  221. END
  222.  
  223. SUB Editline (a$, exitcode%) STATIC
  224.   ' ************************************************
  225.   ' **  Name:          EditLine                   **
  226.   ' **  Type:          Subprogram                 **
  227.   ' **  Module:        EDIT.BAS                   **
  228.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  229.   ' ************************************************
  230.   '
  231.   ' Allows the user to edit a string at the current cursor position
  232.   ' on the screen.  Keys acted upon are Ctrl-Y, Ctrl-Q-Y, Right
  233.   ' arrow, Left arrow, Ctrl-Left arrow, Ctrl-Right arrow, Home, End,
  234.   ' Insert, Escape, Backspace, and Delete.
  235.   ' Pressing Enter, Up arrow, or Down arrow terminates
  236.   ' the subprogram and returns exitCode% of 0, +1, or -1.
  237.   '
  238.   ' EXAMPLE OF USE:  EditLine a$, exitCode%
  239.   ' PARAMETERS:      a$         String to be edited
  240.   '                  exitCode%  Returned code indicating the terminating
  241.   '                             key press
  242.   ' VARIABLES:       row%       Saved current cursor row
  243.   '                  col%       Saved current cursor column
  244.   '                  length%    Length of a$
  245.   '                  ptr%       Location of cursor during the editing
  246.   '                  insert%    Insert mode toggle
  247.   '                  quit%      Flag for quitting the editing
  248.   '                  original$  Saved copy of starting a$
  249.   '                  keyNumber% Integer code for any key press
  250.   '                  ctrlQflag% Indicates Ctrl-Q key press
  251.   '                  kee$       Character of key just pressed
  252.   '                  sp%        Length of space string
  253.   ' MODULE LEVEL
  254.   '   DECLARATIONS:  DECLARE FUNCTION KeyCode% ()
  255.   '                  DECLARE SUB EditLine (a$, exitCode%)
  256.   '
  257.       
  258.   ' Set up some variables
  259.   row% = CSRLIN
  260.   col% = POS(0)
  261.   length% = LEN(a$)
  262.   ptr% = 0
  263.   insert% = TRUE
  264.   quit% = FALSE
  265.   original$ = a$
  266.       
  267. ' Main processing loop
  268.   DO
  269.     
  270.     ' Display the line
  271.       LOCATE row%, col%, 0
  272.       PRINT a$;
  273.     
  274.     ' Show appropriate cursor type
  275.       IF insert% THEN
  276.           LOCATE row%, col% + ptr%, 1, 6, 7
  277.       ELSE
  278.           LOCATE row%, col% + ptr%, 1, 1, 7
  279.       END IF
  280.     
  281.     ' Get next keystroke
  282.       keyNumber% = KeyCode%
  283.     
  284.     ' Process the key
  285.       SELECT CASE keyNumber%
  286.         
  287.       CASE INSERTKEY
  288.           IF insert% THEN
  289.               insert% = FALSE
  290.           ELSE
  291.               insert% = TRUE
  292.           END IF
  293.         
  294.       CASE BACKSPACE
  295.           IF ptr% THEN
  296.               a$ = a$ + " "
  297.               a$ = LEFT$(a$, ptr% - 1) + MID$(a$, ptr% + 1)
  298.               ptr% = ptr% - 1
  299.           END IF
  300.         
  301.       CASE DELETE
  302.           a$ = a$ + " "
  303.           a$ = LEFT$(a$, ptr%) + MID$(a$, ptr% + 2)
  304.         
  305.       CASE UPARROW
  306.           exitcode% = 1
  307.           quit% = TRUE
  308.         
  309.       CASE DOWNARROW
  310.           exitcode% = -1
  311.           quit% = TRUE
  312.         
  313.       CASE LEFTARROW
  314.           IF ptr% THEN
  315.               ptr% = ptr% - 1
  316.           END IF
  317.         
  318.       CASE RIGHTARROW
  319.           IF ptr% < length% - 1 THEN
  320.               ptr% = ptr% + 1
  321.           END IF
  322.         
  323.       CASE ENTER
  324.           exitcode% = 0
  325.           quit% = TRUE
  326.         
  327.       CASE HOME
  328.           ptr% = 0
  329.         
  330.       CASE ENDKEY
  331.           ptr% = length% - 1
  332.         
  333.       CASE CTRLRIGHTARROW
  334.           DO UNTIL MID$(a$, ptr% + 1, 1) = " " OR ptr% = length% - 1
  335.               ptr% = ptr% + 1
  336.           LOOP
  337.           DO UNTIL MID$(a$, ptr% + 1, 1) <> " " OR ptr% = length% - 1
  338.               ptr% = ptr% + 1
  339.           LOOP
  340.         
  341.       CASE CTRLLEFTARROW
  342.           DO UNTIL MID$(a$, ptr% + 1, 1) = " " OR ptr% = 0
  343.               ptr% = ptr% - 1
  344.           LOOP
  345.           DO UNTIL MID$(a$, ptr% + 1, 1) <> " " OR ptr% = 0
  346.               ptr% = ptr% - 1
  347.           LOOP
  348.           DO UNTIL MID$(a$, ptr% + 1, 1) = " " OR ptr% = 0
  349.               ptr% = ptr% - 1
  350.           LOOP
  351.           IF ptr% THEN
  352.               ptr% = ptr% + 1
  353.           END IF
  354.         
  355.       CASE CTRLY
  356.           a$ = SPACE$(length%)
  357.           ptr% = 0
  358.         
  359.       CASE CTRLQ
  360.           ctrlQflag% = TRUE
  361.         
  362.       CASE ESCAPE
  363.           a$ = original$
  364.           ptr% = 0
  365.           insert% = TRUE
  366.         
  367.       CASE IS > 255
  368.           SOUND 999, 1
  369.         
  370.       CASE IS < 32
  371.           SOUND 999, 1
  372.         
  373.       CASE ELSE
  374.         
  375.         ' Convert key code to character string
  376.           kee$ = CHR$(keyNumber%)
  377.         
  378.         ' Insert or overstrike
  379.           IF insert% THEN
  380.               a$ = LEFT$(a$, ptr%) + kee$ + MID$(a$, ptr% + 1)
  381.               a$ = LEFT$(a$, length%)
  382.           ELSE
  383.               IF ptr% < length% THEN
  384.                   MID$(a$, ptr% + 1, 1) = kee$
  385.               END IF
  386.           END IF
  387.         
  388.         ' Are we up against the wall?
  389.           IF ptr% < length% THEN
  390.               ptr% = ptr% + 1
  391.           ELSE
  392.               SOUND 999, 1
  393.           END IF
  394.         
  395.         ' Special check for Ctrl-Q-Y (del to end of line)
  396.           IF kee$ = "y" AND ctrlQflag% THEN
  397.               IF ptr% <= length% THEN
  398.                   sp% = length% - ptr% + 1
  399.                   MID$(a$, ptr%, sp%) = SPACE$(sp%)
  400.                   ptr% = ptr% - 1
  401.               END IF
  402.           END IF
  403.         
  404.         ' Clear out the Ctrl-Q signal
  405.           ctrlQflag% = FALSE
  406.         
  407.       END SELECT
  408.     
  409.   LOOP UNTIL quit%
  410.  
  411. END SUB
  412.  
  413. FUNCTION InKeyCode% STATIC
  414.   ' ************************************************
  415.   ' **  Name:          InKeyCode%                 **
  416.   ' **  Type:          Function                   **
  417.   ' **  Module:        KEYS.BAS                   **
  418.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  419.   ' ************************************************
  420.   '
  421.   ' Returns a unique integer for any key pressed or
  422.   ' a zero if no key was pressed.
  423.   '
  424.   ' EXAMPLE OF USE:  k% = InKeyCode%
  425.   ' PARAMETERS:      (none)
  426.   ' VARIABLES:       (none)
  427.   ' MODULE LEVEL
  428.   '   DECLARATIONS:  DECLARE FUNCTION KeyCode% ()
  429.   '
  430.   InKeyCode% = CVI(INKEY$ + STRING$(2, 0))
  431. END FUNCTION
  432.  
  433. FUNCTION KeyCode% STATIC
  434.   ' ************************************************
  435.   ' **  Name:          KeyCode%                   **
  436.   ' **  Type:          Function                   **
  437.   ' **  Module:        KEYS.BAS                   **
  438.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  439.   ' ************************************************
  440.   '
  441.   ' Returns a unique integer for any key pressed.
  442.   '
  443.   ' EXAMPLE OF USE:  k% = KeyCode%
  444.   ' PARAMETERS:      (none)
  445.   ' VARIABLES:       (none)
  446.   ' MODULE LEVEL
  447.   '   DECLARATIONS:  DECLARE FUNCTION KeyCode% ()
  448.   '
  449.   DO
  450.       k$ = INKEY$
  451.   LOOP UNTIL k$ <> ""
  452.   KeyCode% = CVI(k$ + CHR$(0))
  453. END FUNCTION
  454.  
  455.