home *** CD-ROM | disk | FTP | other *** search
/ PC Format Collection 48 / SENT14D.ISO / tech / delphi / disk15 / stocks.pak / BROWSER.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-08-24  |  6.6 KB  |  268 lines

  1. unit Browser;
  2.  
  3. interface
  4.  
  5. uses WinTypes, WinProcs, Classes, Graphics, Forms, Controls, Buttons,
  6.   StdCtrls, DBCtrls, Grids, DBGrids, ExtCtrls, DB, DBTables, Report;
  7.  
  8. type
  9.   TfmMktBrowser = class(TForm)
  10.     OKBtn: TBitBtn;
  11.     CancelBtn: TBitBtn;
  12.     HelpBtn: TBitBtn;
  13.     Panel1: TPanel;
  14.     Grid: TDBGrid;
  15.     DropIndustry: TComboBox;
  16.     Label1: TLabel;
  17.     DropRating: TComboBox;
  18.     Label2: TLabel;
  19.     DropRecommend: TComboBox;
  20.     DropRisk: TComboBox;
  21.     Label4: TLabel;
  22.     Label3: TLabel;
  23.     GridQuery: TQuery;
  24.     DataSource: TDataSource;
  25.     TempQuery: TQuery;
  26.     ButPrint: TBitBtn;
  27.     Report: TReport;
  28.     procedure DropClick(Sender: TObject);
  29.     procedure ButPrintClick(Sender: TObject);
  30.   private
  31.     IndustCode: string[40];
  32.     procedure PopulateCombos;
  33.     procedure UpdateGridQuery;
  34.   public
  35.     procedure ShowBrowser;
  36.   end;
  37.  
  38. var
  39.   fmMktBrowser: TfmMktBrowser;
  40.  
  41. implementation
  42.  
  43. {$R *.DFM}
  44.  
  45. uses Dialogs,SysUtils;
  46.  
  47. const
  48.   AllStr = 'All';
  49.   FldRecommend = 'RCMNDATION';
  50.   FldRating = 'RATING';
  51.   FldRisk = 'RISK';
  52.   FldIndustry = 'INDUSTRY';
  53.  
  54. procedure TfmMktBrowser.PopulateCombos;
  55.  
  56.   procedure PopulateRatingCombo;
  57.   begin
  58.     DropRating.Items.Clear;
  59.     DropRating.Items.Add(AllStr);
  60.     try
  61.       TempQuery.Close;
  62.       TempQuery.SQL.Clear;
  63.       TempQuery.SQL.Add('Select distinct '+FldRating+' from MASTER');
  64.       TempQuery.Open;
  65.       while not TempQuery.EOF do
  66.       begin
  67.         DropRating.Items.Add(TempQuery.Fields[0].AsString);
  68.         TempQuery.Next;
  69.       end;
  70.       DropRating.ItemIndex := 0;
  71.     except
  72.       on EDatabaseError do
  73.         MessageDlg('Error determining Industry categories', mtError, [mbOK], 0);
  74.     end;
  75.   end;
  76.  
  77.   procedure PopulateRecommendCombo;
  78.   begin
  79.     DropRecommend.Items.Clear;
  80.     DropRecommend.Items.Add(AllStr);
  81.     try
  82.       TempQuery.Close;
  83.       TempQuery.SQL.Clear;
  84.       TempQuery.SQL.Add('Select distinct '+FldRecommend+' from MASTER');
  85.       TempQuery.Open;
  86.       while not TempQuery.EOF do
  87.       begin
  88.         DropRecommend.Items.Add(TempQuery.Fields[0].AsString);
  89.         TempQuery.Next;
  90.       end;
  91.       DropRecommend.ItemIndex := 0;
  92.     except
  93.       on EDatabaseError do
  94.         MessageDlg('Error determining Industry categories', mtError, [mbOK], 0);
  95.     end;
  96.   end;
  97.  
  98.   procedure PopulateRiskCombo;
  99.   begin
  100.     DropRisk.Items.Clear;
  101.     DropRisk.Items.Add(AllStr);
  102.     try
  103.       TempQuery.Close;
  104.       TempQuery.SQL.Clear;
  105.       TempQuery.SQL.Add('Select distinct '+FldRisk+' from MASTER');
  106.       TempQuery.Open;
  107.       while not TempQuery.EOF do
  108.       begin
  109.          DropRisk.Items.Add(TempQuery.Fields[0].AsString);
  110.          TempQuery.Next;
  111.       end;
  112.       DropRisk.ItemIndex := 0;
  113.     except
  114.       on EDatabaseError do
  115.         MessageDlg('Error determining Industry categories', mtError, [mbOK], 0);
  116.     end;
  117.   end;
  118.  
  119.   procedure PopulateIndustryCombo;
  120.   begin
  121.     DropIndustry.Items.Clear;
  122.     DropIndustry.Items.Add(AllStr);
  123.     try
  124.       TempQuery.Close;
  125.       TempQuery.SQL.Clear;
  126.       TempQuery.SQL.Add('Select LONG_NAME from INDUSTRY');
  127.       TempQuery.Open;
  128.       while not TempQuery.EOF do
  129.       begin
  130.          DropIndustry.Items.Add(TempQuery.Fields[0].AsString);
  131.          TempQuery.Next;
  132.       end;
  133.       DropIndustry.ItemIndex := 0;
  134.     except
  135.       on EDatabaseError do
  136.         MessageDlg('Error determining Industry categories', mtError, [mbOK], 0);
  137.     end;
  138.   end;
  139.  
  140. begin
  141.   TempQuery.Close;
  142.   TempQuery.DatabaseName := 'StockDB';
  143.   PopulateRatingCombo;
  144.   PopulateRecommendCombo;
  145.   PopulateRiskCombo;
  146.   PopulateIndustryCombo;
  147. end;
  148.  
  149. procedure TfmMktBrowser.UpdateGridQuery;
  150. var
  151.   WhereUsed: Boolean;
  152.  
  153.   function SQLPrefix:string;
  154.   begin
  155.     if WhereUsed then
  156.       SQLPrefix := 'and '
  157.     else
  158.     begin
  159.       WhereUsed := true;
  160.       SQLPrefix := 'where '
  161.     end;
  162.   end;
  163.  
  164. begin
  165.   Screen.Cursor := crHourGlass;
  166.   WhereUsed := false;
  167.   with GridQuery do
  168.   begin
  169.     Close;
  170.     SQL.Clear;
  171.     SQL.Add('select * from MASTER');
  172.     if DropIndustry.ItemIndex > 0 then
  173.     begin
  174.       { lookup the industry code from the INDUSTRY table }
  175.       TempQuery.Close;
  176.       TempQuery.SQL.Clear;
  177.       TempQuery.SQL.Add('Select IND_CODE from INDUSTRY');
  178.       TempQuery.SQL.Add('where LONG_NAME ="'+DropIndustry.Items[DropIndustry.ItemIndex]+'"');
  179.       TempQuery.Open;
  180.       { store the industry code; used for RS report }
  181.       IndustCode := TempQuery.Fields[0].AsString;
  182.       { now add the clause to the Grid SQL }
  183.       SQL.Add(SQLPrefix+FldIndustry+' ='+IndustCode);
  184.     end;
  185.     if DropRating.ItemIndex > 0 then
  186.       SQL.Add(SQLPrefix+FldRating+' ="'+DropRating.Items[DropRating.ItemIndex]+'"');
  187.     if DropRecommend.ItemIndex > 0 then
  188.       SQL.Add(SQLPrefix+FldRecommend+' ="'+DropRecommend.Items[DropRecommend.ItemIndex]+'"');
  189.     if DropRisk.ItemIndex > 0 then
  190.       SQL.Add(SQLPrefix+FldRisk+' ="'+DropRisk.Items[DropRisk.ItemIndex]+'"');
  191.     SQL.Add('order by CO_NAME');
  192.     Open;
  193.   end;
  194.   Screen.Cursor := crDefault;
  195. end;
  196.  
  197. procedure TfmMktBrowser.ShowBrowser;
  198. begin
  199.   if not Visible then
  200.   begin
  201.     PopulateCombos;
  202.     UpdateGridQuery;
  203.   end;
  204.   Show;
  205. end;
  206.  
  207. procedure TfmMktBrowser.DropClick(Sender: TObject);
  208. begin
  209.   UpdateGridQuery;
  210. end;
  211.  
  212. { the report variables passed to ReportSmith are actually
  213.   SQL clauses }
  214. procedure TfmMktBrowser.ButPrintClick(Sender: TObject);
  215. const
  216.   NotNull = 'IS NOT NULL>';
  217.   IndustVal = '@INDUST=<';
  218.   RatingVal = '@RATING=<';
  219.   RiskVal = '@RISK=<';
  220.   RecmVal = '@RECM=<';
  221.  
  222.   function IndustValue:string;
  223.   begin
  224.     if DropIndustry.ItemIndex = 0 then
  225.       IndustValue := IndustVal+NotNull
  226.     else
  227.       IndustValue := IndustVal+'='+InDustCode+'>';
  228.   end;
  229.  
  230.   function RatingValue:string;
  231.   begin
  232.     if DropRating.ItemIndex = 0 then
  233.       RatingValue := RatingVal+NotNull
  234.     else
  235.       RatingValue := RatingVal+'='''+DropRating.Items[DropRating.ItemIndex]+'''>';
  236.   end;
  237.  
  238.   function RiskValue:string;
  239.   begin
  240.     if DropRisk.ItemIndex = 0 then
  241.       RiskValue := RiskVal+NotNull
  242.     else
  243.       RiskValue := RiskVal+'='''+DropRisk.Items[DropRisk.ItemIndex]+'''>';
  244.   end;
  245.  
  246.   function RecmValue:string;
  247.   begin
  248.     if DropRecommend.ItemIndex = 0 then
  249.       RecmValue := RecmVal+NotNull
  250.     else
  251.       RecmValue := RecmVal+'='''+DropRecommend.Items[DropRecommend.ItemIndex]+'''>';
  252.   end;
  253.  
  254. begin
  255.   with Report do
  256.   begin
  257.     ReportDir := ExtractFilePath(Application.ExeName);
  258.     InitialValues.Clear;
  259.     InitialValues.Add(IndustValue);
  260.     InitialValues.Add(RatingValue);
  261.     InitialValues.Add(RiskValue);
  262.     InitialValues.Add(RecmValue);
  263.     Run;
  264.   end;
  265. end;
  266.  
  267. end.
  268.