home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / kompon / d23456 / BKQUERY.ZIP / uEQueryForm.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2000-11-03  |  6.2 KB  |  247 lines

  1. unit uEQueryForm;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   Grids, ExtCtrls, Db, DBTables, StdCtrls, Buttons, Spin, Mask, ComCtrls;
  8.  
  9. type
  10.   TF_EQRef = class(TForm)
  11.     Panel1: TPanel;
  12.     StringGrid1: TStringGrid;
  13.     Panel2: TPanel;
  14.     SpeedButton1: TSpeedButton;
  15.     ListBox1: TListBox;
  16.     Panel3: TPanel;
  17.     Button1: TButton;
  18.     Button2: TButton;
  19.     Label1: TLabel;
  20.     Label2: TLabel;
  21.     procedure FormActivate(Sender: TObject);
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure SpeedButton1Click(Sender: TObject);
  24.     procedure Label2DblClick(Sender: TObject);
  25.     procedure ListBox1DblClick(Sender: TObject);
  26.     procedure Button1Click(Sender: TObject);
  27.     procedure Button2Click(Sender: TObject);
  28.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  29.   private
  30.     fCancel:boolean;
  31.     fActive:boolean;
  32.     fFirst:boolean;
  33.     fQ:TQuery;
  34.     oForm:TForm;
  35.     fS:TStringList;
  36.     procedure MyList;
  37.     procedure Zapis;
  38.     { Private declarations }
  39.   public
  40.     { Public declarations }
  41.   end;
  42.  
  43. var
  44.   F_EQRef: TF_EQRef;
  45.  
  46.   procedure Execute_EQRef(pQ:TQuery; pS:TStringList );
  47.   procedure Execute_EQOpen(pQ:TQuery; pS:TStringList);
  48.   procedure Execute_EQClose;
  49.  
  50. implementation
  51.  
  52. {$R *.DFM}
  53.  
  54. procedure Execute_EQRef(pQ:TQuery; pS:TStringList);
  55. begin
  56.   if F_EQRef=nil then Application.CreateForm(TF_EQRef,F_EQRef) else begin
  57.     if F_EQRef.fActive then begin
  58.       F_EQRef.Close;
  59.       F_EQRef.Free;
  60.       F_EQRef:=nil;
  61.       Application.CreateForm(TF_EQRef,F_EQRef)
  62.     end;
  63.   end;
  64.   try
  65.     F_EQRef.fActive:=false;
  66.     F_EQRef.ModalResult:=mrCancel;
  67.     F_EQRef.fQ:=pQ;
  68.     F_EQRef.fS:=pS;
  69.     F_EQRef.oForm:=TForm(pQ.Owner);
  70.     F_EQRef.fQ:=pQ;
  71.     F_EQRef.ShowModal;
  72.   finally
  73.     F_EQRef.Free;
  74.     F_EQRef:=nil;
  75.   end;
  76. end;
  77.  
  78. procedure Execute_EQOpen(pQ:TQuery; pS:TStringList);
  79. begin
  80.   if F_EQRef=nil then Application.CreateForm(TF_EQRef,F_EQRef);
  81.   F_EQRef.fQ:=pQ;
  82.   F_EQRef.fS:=pS;
  83.   F_EQRef.oForm:=TForm(pQ.Owner);
  84.   F_EQRef.fFirst:=true;
  85.   F_EQRef.fActive:=true;
  86.   F_EQRef.Show;
  87. end;
  88.  
  89. procedure Execute_EQClose;
  90. begin
  91.   if Assigned(F_EQRef) then begin
  92.     F_EQRef.fActive:=false;
  93.     F_EQRef.Close;
  94.   end;
  95. end;
  96.  
  97. procedure TF_EQRef.FormActivate(Sender: TObject);
  98. begin
  99.   if fFirst then begin
  100.     fFirst:=false;
  101.     MyList;
  102.   end;
  103. end;
  104.  
  105. procedure TF_EQRef.MyList;
  106. var i,j,k:integer;
  107.     mS,mT1,mT2:string;
  108.     mB:Boolean;
  109. begin
  110.   if fQ=nil then begin
  111.     ShowMessage('EQuery?Dialog - EQuery is nil');
  112.     Postmessage(Handle,wm_close,0,0);
  113.     exit;
  114.   end;
  115.   if oForm=nil then begin
  116.     ShowMessage(fQ.Name+'Dialog - Form=nil');
  117.     Postmessage(Handle,wm_close,0,0);
  118.     exit;
  119.   end;
  120.   if fS=nil then begin
  121.     ShowMessage(fQ.Name+'Dialog - StringList=nil');
  122.     Postmessage(Handle,wm_close,0,0);
  123.     exit;
  124.   end;
  125.   ListBox1.Clear;
  126.   ListBox1.Items.Add('');
  127.   for i:=0 to oForm.componentCount-1do begin
  128.     if (oForm.Components[i] is Tedit) or
  129.        (oForm.Components[i] is TComboBox) or
  130.        (oForm.Components[i] is TSpinEdit) or
  131.        (oForm.Components[i] is TMemo) or
  132.        (oForm.Components[i] is TMaskedit) or
  133.        (oForm.Components[i] is TdateTimePicker) or
  134.        (oForm.Components[i] is TCheckBox) then begin
  135.        ListBox1.Items.Add(oForm.Components[i].name);
  136.     end;
  137.   end;
  138.   ListBox1.Items.Add('{Another}');
  139.   ListBox1.ItemIndex:=0;
  140.   with StringGrid1 do begin
  141.     cells[0,0]:='Query Fields';
  142.     cells[1,0]:='Ref. Components';
  143.     mB:=fQ.Active;
  144.     try
  145.       fQ.Active:=true;
  146.       for i:=0 to fS.Count-1 do begin
  147.         mS:=trim(fS.Strings[i]); mT1:=''; mT2:='';
  148.         if mS<>'' then begin
  149.           j:=Pos('=',mS);
  150.           if j>1 then begin
  151.             mT1:=copy(mS,1,j-1); mT2:=copy(mS,j+1,255);
  152.           end else begin
  153.             mT2:=mS;
  154.             if mT2[1]='=' then delete(mT2,1,1);
  155.           end;
  156.           mT1:=Trim(mT1); mT2:=Trim(mT2);
  157.         end;
  158.         if trim(mT1)='' then continue;
  159.         if fQ.FindField(mT1)=nil then Cells[0,i+1]:='? '+mT1 else Cells[0,i+1]:=mT1;
  160.         if Pos('{',mT2)>0 then Cells[1,i+1]:=mT2 else begin
  161.           if Trim(mT2)='' then cells[1,i+1]:='' else begin
  162.             if oForm.FindComponent(mT2)=nil then Cells[1,i+1]:='? '+mT2 else Cells[1,i+1]:=mT2;
  163.           end;
  164.         end;
  165.       end;
  166.       j:=fS.Count;
  167.       for i:=0 to fQ.Fields.count-1 do begin
  168.         k:=fS.IndexOfName(fQ.Fields[i].FieldName);
  169.         if k>-1 then begin
  170.           cells[0,k+1]:=fQ.Fields[i].FieldName;
  171.           continue;
  172.         end;
  173.         inc(j);
  174.         cells[0,j]:=fQ.Fields[i].FieldName;
  175.       end;
  176.     finally
  177.       if not fQ.active then begin
  178.         ShowMessage(fQ.Name+' query - open error');
  179.         Postmessage(Handle,wm_close,0,0);
  180.       end;
  181.       if not mB then fQ.Active:=false;
  182.     end;
  183.   end;
  184. end;
  185.  
  186. procedure TF_EQRef.FormCreate(Sender: TObject);
  187. begin
  188.   fFirst:=true;
  189.   //fS:=TStringList.Create;
  190.   //fS.Clear;
  191.   //fForm:=self;
  192. end;
  193.  
  194. procedure TF_EQRef.SpeedButton1Click(Sender: TObject);
  195. begin
  196.   if StringGrid1.cells[1,StringGrid1.row]<>'' then Label2.caption:=StringGrid1.cells[1,StringGrid1.row];
  197.   StringGrid1.cells[1,StringGrid1.row]:=ListBox1.Items[ListBox1.ItemIndex];
  198. end;
  199.  
  200. procedure TF_EQRef.Label2DblClick(Sender: TObject);
  201. begin
  202.   StringGrid1.cells[1,StringGrid1.row]:=Label2.caption;
  203. end;
  204.  
  205. procedure TF_EQRef.ListBox1DblClick(Sender: TObject);
  206. begin
  207.   SpeedButton1Click(sender);
  208. end;
  209.  
  210. procedure TF_EQRef.Button1Click(Sender: TObject);
  211. begin
  212.   fCancel:=false;
  213.   close;
  214. end;
  215.  
  216. procedure TF_EQRef.Button2Click(Sender: TObject);
  217. begin
  218.   fCancel:=true;
  219.   close;
  220. end;
  221.  
  222. procedure TF_EQRef.Zapis;
  223. var i:integer;
  224.     mT1,mt2,mS:String;
  225. begin
  226.   fS.Clear; mS:='';
  227.   with StringGrid1 do begin
  228.     for i:=1 to RowCount-1 do begin
  229.       mT1:=cells[0,i];
  230.       mT2:=cells[1,i];
  231.       while Pos('?',mT1)>0 do delete(mt1,Pos('?',mt1),1);
  232.       while Pos('?',mT2)>0 do delete(mt2,Pos('?',mt2),1);
  233.       mT1:=Trim(mT1); mT2:=Trim(mT2);
  234.       if mT1<>'' then mS:=mS+mT1+'='+mT2+#13#10;
  235.     end;
  236.   end;
  237.   fS.text:=mS;
  238. end;
  239.  
  240. procedure TF_EQRef.FormClose(Sender: TObject; var Action: TCloseAction);
  241. begin
  242.    if not fCancel then Zapis;
  243. end;
  244.  
  245. end.
  246.  
  247.