home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Environments / TurPasDBTlbx / TP-Database Toolbox / Access Samples / HITADemo.pas next >
Encoding:
Pascal/Delphi Source File  |  1987-12-11  |  13.1 KB  |  409 lines  |  [TEXT/TPAS]

  1. (*********************************************************************)
  2. (*                  Turbo Pascal Database Toolbox                    *)
  3. (*                       For the Macintosh                           *)
  4. (*            Copyright (C) 1987 Borland International               *)
  5. (*                     Toolbox version: 1.0                          *)
  6. (*                                                                   *)
  7. (*            Turbo Access High-Level Demonstration Program          *)
  8. (*                                                                   *)
  9. (*        Purpose: Shows how to implement a sample database using    *)
  10. (*                 the Turbo Access high-level calls.                *)
  11. (*                                                                   *)
  12. (*********************************************************************)
  13.  
  14. (*********************************************************************)
  15. (*              HITADemo - Configuration for compilation             *)
  16. (*                                                                   *)
  17. (*    Follow these steps in order to compile HITADemo.pas:           *)
  18. (*                                                                   *)
  19. (*    1.  Copy TAccess.unit and TAHigh.unit from the Turbo Access    *)
  20. (*        folder into the folder or disk that contains HITADemo.pas. *)
  21. (*                                                                   *)
  22. (*    2.  Bring the source for TAccess.unit into the Turbo Pascal    *)
  23. (*        integrated environment.                                    *)
  24. (*                                                                   *)
  25. (*    3.  Modify the $I include file directive to this syntax:       *)
  26. (*        {$I TADemo.const}                                          *)
  27. (*                                                                   *)
  28. (*    4.  Compile TAccess.unit to disk.                              *)
  29. (*                                                                   *)
  30. (*    5.  Compile TAHigh.unit to disk.                               *)
  31. (*                                                                   *)
  32. (*    5.  Bring the HITADemo.pas source file into the Turbo Pascal   *)
  33. (*        environment.                                               *)
  34. (*                                                                   *)
  35. (*    6.  Run HITADemo in memory or compile to disk.                 *)
  36. (*                                                                   *)
  37. (*    For further reference, see pages 34-39 of the Turbo Pascal     *)
  38. (*    Database Toolbox Owner's Handbook.                             *)
  39. (*                                                                   *)
  40. (*********************************************************************)
  41.  
  42. Program HiTADemo;
  43. {$R TAccess.RSRC} { Contains Turbo Access error messages }
  44. {$U TAccess}
  45. {$U TAHigh}              { Use the high-level calls unit }
  46. uses 
  47.  {If a compiler error occurs here, Turbo Pascal cannot find the TAccess
  48.   and TAHigh units. You must first compile these units to this disk (or 
  49.   folder if using HFS) before compiling HiTADemo. See the top of this file
  50.   for detailed instructions.
  51.  }
  52.  
  53.    MemTypes, QuickDraw, OSIntF, ToolIntf, PackIntf, TAccess, TAHigh;
  54.  
  55. {$I TADemo.types} { Same type definition file as for TADemo.pas }
  56.  
  57. const
  58.   DataFileNm  = 'Customers.data';
  59.   IndexFileNm = 'Customers.index';
  60.   
  61.   
  62. type
  63.   Filename = string[66];
  64.  
  65.  
  66. (**************************************************************)
  67. (*  Global variable are declared here.                        *)
  68. (**************************************************************)  
  69. var
  70.   Customers : DataSet;
  71.   CustRecord : CustRec;
  72.  
  73. (***********************************************************************)
  74. (*  Utility procedures which can be called from all other procedures   *)
  75. (***********************************************************************)
  76. function UpCase(ch : char): char; 
  77. { Returns the upper case equivalent of ch } 
  78. inline 
  79.    $301F, { UpCase MOVE.W (SP)+,D0 ; GetCh }
  80.    $0C40,
  81.    $0061, { CMP.W #'a',D0 ; skip if not lower case }
  82.    $6D0A, { BLT.S @1 }
  83.    $0C40,
  84.    $007A, { CMP.W #'z',D0 }
  85.    $6E04, { BGT.S @1 }
  86.    $0440, 
  87.    $0020, { SUB.W #$20,D0 }
  88.    $3E80; { @1 MOVE.W D0,(SP) }
  89.  
  90.  
  91. function Confirmed : boolean;
  92. { Returns true if user types 'Y' to signify yes }
  93. var
  94.   Response : char;
  95. begin
  96.   repeat
  97.     Response := UpCase(ReadChar);
  98.     if not (Response in ['Y','N']) then
  99.       SysBeep(1);
  100.   until Response in ['Y','N'];
  101.   Confirmed := Response = 'Y';
  102.   Writeln;
  103. end; { Confirmed }
  104.  
  105. procedure Pause;
  106. var
  107.   ch : char;
  108. begin
  109.   Writeln;
  110.   Write(' Press any key to continue . . .');
  111.   ch := ReadChar;
  112. end; { Pause }
  113.  
  114. procedure Abort(Message : string);
  115. { Reports the error message to the user and then halts }
  116. begin
  117.   GotoXY(1, 24);
  118.   Write(^G, Message, ', Hit any key to halt.');
  119.   repeat until KeyPressed;
  120.   Halt;
  121. end;
  122.  
  123. procedure InputInformation(var CustRecord : CustRec);
  124. { Obtain CustRecord information from the user to put in the data base }
  125. begin
  126.   Writeln;
  127.   Writeln(' Enter customer Information ');
  128.   Writeln;
  129.   with CustRecord do
  130.   begin
  131.     CustStatus := 0;
  132.     Write('customer code: '); Readln(CustCode);
  133.     Write('Entry date   : '); Readln(EntryDate);
  134.     Write('First name   : '); Readln(FirstName);
  135.     Write('Last name    : '); Readln(LastName);
  136.     Write('Company      : '); Readln(Company);
  137.     Writeln('Address ');
  138.     Write('   Number & Street   : '); Readln(Addr1);
  139.     Write('   City, State & Zip : '); Readln(Addr2);
  140.     Write('Phone     : '); Readln(Phone);
  141.     Write('Extention : '); Readln(PhoneExt);
  142.     Write('Remarks   : '); Readln(Remarks1);
  143.     Write('Remarks   : '); Readln(Remarks2);
  144.     Write('Remarks   : '); Readln(Remarks3);
  145.   end;
  146.   Writeln;
  147. end; { InputInformation }
  148.  
  149.  
  150. (***********************************************************************)
  151. (*  Place the customer information on the screen to be viewed          *)
  152. (***********************************************************************)
  153. procedure DisplayCustomer(CustRecord: CustRec);
  154. begin
  155.   with CustRecord do
  156.   begin
  157.     Writeln;
  158.     WriteLn('   Code: ',CustCode,'    Date: ',EntryDate);
  159.     Writeln('   Name: ',FirstName,' ',LastName);
  160.     WriteLn('Company: ',Company);
  161.     Writeln('Address: ',Addr1);
  162.     Writeln('         ',Addr2);
  163.     Writeln('  Phone:',Phone,' ext. ',PhoneExt);
  164.     WriteLn('Remarks: ',Remarks1);
  165.     Writeln('         ',Remarks2);
  166.     WriteLn('         ',Remarks3);
  167.   end;
  168.   Writeln;
  169. end; { Display customer }
  170.  
  171. procedure ListCustomers(var Customers: DataSet);
  172. { Lists customer records (ordered by customer codes) on the screen }
  173. var
  174.   Count : LongInt;
  175.   TempCode : CodeStr;
  176. begin
  177.   Count := 0;
  178.   TAReset(Customers);
  179.   repeat
  180.     TANext(Customers, CustRecord, TempCode);
  181.     if Ok then
  182.     begin
  183.       DisplayCustomer(CustRecord);
  184.       Count := succ(Count);
  185.     end;
  186.   until not Ok;
  187.   if Count > 0 then
  188.   begin
  189.     Writeln;
  190.     Writeln(Count, ' total customers');
  191.   end;
  192. end; { ListCustomers }
  193.  
  194.  
  195. procedure FindCustomer(var Customers: DataSet;
  196.                        Exact : boolean);
  197. { Find customer based on customer code.  If Exact is true,
  198.   TARead will be called with an exact match criterion otherwise
  199.   a partial matches will satisfy the search.                        
  200. }
  201. var
  202.   SearchCode : CodeStr;
  203.  
  204. begin
  205.   Write('Enter the customer code: '); 
  206.   ReadLn(SearchCode);
  207.   TARead(Customers, CustRecord, SearchCode, Exact);
  208.   if OK then
  209.     DisplayCustomer(CustRecord)
  210.   else
  211.     Writeln('A record was not found for the key ',SearchCode);
  212. end { FindCustomer };
  213.  
  214. procedure NextCustomer(var Customers: DataSet);
  215. { Next customer based on customer code  }
  216. var
  217.   CustomerCode   : CodeStr;
  218. begin
  219.   TANext(Customers,CustRecord, CustomerCode);
  220.   if OK then
  221.     DisplayCustomer(CustRecord)
  222.   else
  223.     Writeln('The end of the database has been reached.');
  224. end; { Next customer }
  225.  
  226. procedure PreviousCustomer(var Customers: DataSet);
  227. { Previous customer based on customer code. }
  228. var
  229.   TempCode : CodeStr;
  230. begin
  231.   TAPrev(Customers, CustRecord, TempCode);
  232.   if OK then
  233.     DisplayCustomer(CustRecord)
  234.   else
  235.     Writeln('The start of the database has been reached.');
  236. end { Previous customer };
  237.  
  238. procedure AddCustomer(var Customers: DataSet);
  239. { AddCustomer inserts records into the data file and keys into the index }
  240. var
  241.   TempCode        : CodeStr;
  242. begin
  243.   repeat
  244.     InputInformation(CustRecord);
  245.     TempCode := CustRecord.CustCode;
  246.     TAInsert(Customers, CustRecord, TempCode);
  247.     if Ok then
  248.       Write('Add another record? ')
  249.     else
  250.       Write('Duplicate code exists. Try another code? ');
  251.   until not Confirmed;
  252. end; { AddCustomer }
  253.  
  254. procedure DeleteCustomer(var Customers: DataSet);
  255. { DeleteCustomer accepts the customer code and deletes data and key info. }
  256. var
  257.   CustomerCode    : CodeStr;
  258. begin
  259.   repeat
  260.     Write(' Enter code of customer to be deleted: '); 
  261.     Readln(CustomerCode);
  262.     TADelete(Customers, CustomerCode);
  263.     if Ok then
  264.       Write('Delete another record? ')
  265.     else
  266.       Write('customer code was not fould. Try another code? ');
  267.   until not Confirmed;
  268. end { DeleteCustomer };
  269.  
  270. procedure UpdateCustomer(var  Customers : DataSet);
  271. { UpdateCustomer show a customer and then allow reentry of information }
  272. var
  273.   SearchCode    : CodeStr;
  274.   
  275. begin
  276.   repeat
  277.     Write('Enter code of customer to be updated: ');
  278.     Readln(SearchCode);
  279.     TARead(Customers, CustRecord, SearchCode, ExactMatch);
  280.     if Ok then
  281.     begin
  282.       DisplayCustomer(CustRecord);
  283.       InputInformation(CustRecord);
  284.       if SearchCode = CustRecord.CustCode then
  285.         TAUpdate(Customers, CustRecord, SearchCode)
  286.       else
  287.       begin
  288.         TAInsert(Customers, CustRecord, CustRecord.CustCode);
  289.         if Ok then
  290.           TADelete(Customers, SearchCode)
  291.         else
  292.           Writeln('Customer Code already used');
  293.       end;
  294.       Write('Update another record? ');
  295.     end
  296.     else
  297.       Write('customer code was not found. Try another code? ');
  298.   until not Confirmed;  
  299. end; { Update customer }
  300.  
  301.  
  302. (****************************************************************************)
  303. (*  Rebuild's the Data set's index file from the data file                  *)
  304. (****************************************************************************)
  305. procedure RebuildDatabase(var Customers : DataSet);
  306.  
  307. procedure RebuildIndex(VAR CustFile: DataFile;
  308.                        VAR CodeIndex: IndexFile;
  309.                        FileNm : FileName);
  310. var
  311.   RecordNumber : LongInt;
  312. begin
  313.   MakeIndex(CodeIndex,FileNm,
  314.             SizeOf(CustRecord.CustCode)-1,NoDuplicates);
  315.   if not Ok then
  316.     Abort('Could not Rebuild index file ' + FileNm);
  317.   for RecordNumber := 1 to FileLen(CustFile) - 1 do
  318.   begin
  319.     GetRec(CustFile,RecordNumber, CustRecord);
  320.     If (CustRecord.CustStatus = 0) then
  321.       AddKey(CodeIndex,RecordNumber,CustRecord.CustCode);
  322.   end
  323. end; { RebuildIndex }
  324.  
  325. begin { RebuildDatabase }
  326.   with Customers do
  327.   begin
  328.     CloseIndex(Index);
  329.     RebuildIndex(Data, Index, IndexFileNm);
  330.   end;
  331. end; { RebuildDatabase }
  332.  
  333.  
  334. (*******************************************************************)
  335. (*                          Main menu                              *)
  336. (*******************************************************************)
  337. function Menu: char;
  338. var
  339.   action: char;
  340. begin
  341.   ClearScreen;
  342.   GotoXY(1,3);
  343.   Writeln('   Enter Number or First Letter');
  344.   Writeln;
  345.   Writeln(' 1)  List customer Records ');
  346.   Writeln(' 2)  Find a Record by customer Code ');
  347.   Writeln(' 3)  Search on Partial customer Code ');
  348.   Writeln(' 4)  Next customer');
  349.   Writeln(' 5)  Previous customer');
  350.   Writeln(' 6)  Add to customer Database ');
  351.   Writeln(' 7)  Update a customer Record ');
  352.   Writeln(' 8)  Delete a customer Record ');
  353.   Writeln(' 9)  Rebuild Index file ');
  354.   Writeln(' 0)  Exit ');
  355.   Writeln(' ');
  356.   Action := ReadChar;
  357.   Writeln;
  358.   Menu := UpCase(action);
  359. end { menu };
  360.  
  361. procedure CleanUp;
  362. begin
  363.   TAClose(Customers);
  364. end;
  365.  
  366. procedure SetUpDatabase(var Customers : DataSet);
  367. { Opens up data set (Data and index files) if the files
  368.   exist otherwise the data set is created. }
  369. begin
  370.   TAOpen(Customers, DataFileNm,SizeOf(CustRec),
  371.                     IndexFileNm, SizeOf(CodeStr) - 1);
  372.   if not Ok then
  373.      TACreate(Customers, DataFileNm,SizeOf(CustRec),
  374.               IndexFileNm, SizeOf(CodeStr) - 1);
  375.   if not Ok then
  376.     Abort('Could not create the data set');
  377.     TAErrorProc := @CleanUp;
  378.   { Set up fatal error handler so it will close the database files. }
  379. end; { SetUpDatabase }
  380.  
  381. (***********************************************************************)
  382. (*                            Main program                             *)
  383. (***********************************************************************)
  384. var
  385.   Finished: Boolean;
  386.  
  387. begin
  388.   SetUpDatabase(Customers);
  389.   Finished := false;
  390.   repeat
  391.     case Menu of
  392.       '1','L': ListCustomers(Customers);
  393.       '2','F': FindCustomer(Customers, ExactMatch);
  394.       '3','S': FindCustomer(Customers, PartialMatch);
  395.       '4','N': NextCustomer(Customers);
  396.       '5','P': PreviousCustomer(Customers);
  397.       '6','A': AddCustomer(Customers);
  398.       '7','U': UpdateCustomer(Customers);
  399.       '8','D': DeleteCustomer(Customers);
  400.       '9','R': RebuildDatabase(Customers);
  401.       '0','E': Finished := true;
  402.       otherwise Write('Choose 0-9: ');
  403.     end; { case }
  404.     if not Finished then
  405.       Pause;
  406.   until Finished;
  407.   CleanUp;
  408. end.
  409.