unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, Mask, StdCtrls, Buttons, DBTables, Grids, DBGrids, ComObj,ComCtrls;

type
  TForm3 = class(TForm)
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Query1: TQuery;
    Query2: TQuery;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    BitBtn4: TBitBtn;
    BitBtn5: TBitBtn;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Query3: TQuery;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    ComboBox3: TComboBox;
    MaskEdit1: TMaskEdit;
    MaskEdit2: TMaskEdit;
    MaskEdit3: TMaskEdit;
    Query1Id: TIntegerField;
    Query1PIB: TStringField;
    Query1DataNarodgennya: TDateField;
    Query1PasportniDani: TStringField;
    Query1IPN: TFloatField;
    Query1DataVlash: TDateField;
    Query1Adresa: TStringField;
    Query1Telefon: TStringField;
    Query1IdViddily: TIntegerField;
    Query1IdPosadu: TIntegerField;
    Query1Oklad: TFloatField;
    Query1IdMagazuny: TStringField;
    Query1NazvaViddily: TStringField;
    Query1NazvaPosadu: TStringField;
    Query1NazvaMagazuny: TStringField;
    Query4: TQuery;
    Query5: TQuery;
    GroupBox3: TGroupBox;
    Label12: TLabel;
    Label13: TLabel;
    BitBtn6: TBitBtn;
    DateTimePicker1: TDateTimePicker;
    DateTimePicker2: TDateTimePicker;
    GroupBox4: TGroupBox;
    BitBtn7: TBitBtn;
    BitBtn8: TBitBtn;
    BitBtn9: TBitBtn;
    GroupBox5: TGroupBox;
    GroupBox6: TGroupBox;
    ComboBox4: TComboBox;
    ComboBox5: TComboBox;
    Query6: TQuery;
    Query7: TQuery;
    Query6Id: TIntegerField;
    Query6NazvaViddily: TStringField;
    Query6MisceRoztash: TStringField;
    Query7Id: TIntegerField;
    Query7NazvaMagazuny: TStringField;
    Query7AdresaMagazuny: TStringField;
    Query7IdSpivrobitnuka: TIntegerField;
    BitBtn10: TBitBtn;
    BitBtn11: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure Edit4KeyPress(Sender: TObject; var Key: Char);
    procedure Edit7KeyPress(Sender: TObject; var Key: Char);
    procedure DateTimePicker1CloseUp(Sender: TObject);
    procedure DateTimePicker2CloseUp(Sender: TObject);
    procedure BitBtn6Click(Sender: TObject);
    procedure Filter();
    procedure FormCreate(Sender: TObject);
    procedure ComboBox4CloseUp(Sender: TObject);
    procedure ComboBox5CloseUp(Sender: TObject);
    procedure BitBtn10Click(Sender: TObject);
    procedure BitBtn7Click(Sender: TObject);
    procedure BitBtn8Click(Sender: TObject);
    procedure BitBtn9Click(Sender: TObject);
    procedure BitBtn11Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;
  IdVi,IdPo,IdMa:array of string;
  mas,masb,m:array of string;
  i,j:integer;

implementation

uses Unit5, Unit6;

{$R *.dfm}

procedure TForm3.BitBtn1Click(Sender: TObject);
var n,k,j:integer;
s:string;
begin
Query2.SQL.Clear;
Query2.SQL.Add('SELECT* FROM viddilu');
Query2.Active:=False;
Query2.Active:=True;
ComboBox1.Items.Clear;
finalize(IdVi);
if not Query2.IsEmpty then repeat
setLength(IdVi,Length(IdVi)+1);
IdVi[high(IdVi)]:= Query2.FieldByName('Id').AsString;
ComboBox1.Items.Add(Query2.FieldByName('NazvaViddily').AsString);
until Query2.MoveBy(1)<1;
     ComboBox1.ItemIndex:=-1;
     ComboBox1.Text:='';

Query3.SQL.Clear;
Query3.SQL.Add('SELECT* FROM posadu');
Query3.Active:=False;
Query3.Active:=True;
ComboBox2.Items.Clear;
finalize(IdPo);
if not Query3.IsEmpty then repeat
setLength(IdPo,Length(IdPo)+1);
IdPo[high(IdPo)]:= Query3.FieldByName('Id').AsString;
ComboBox2.Items.Add(Query3.FieldByName('NazvaPosadu').AsString);
until Query3.MoveBy(1)<1;
     ComboBox2.ItemIndex:=-1;
     ComboBox2.Text:='';

Query4.SQL.Clear;
Query4.SQL.Add('SELECT* FROM magazunu');
Query4.Active:=False;
Query4.Active:=True;
ComboBox3.Items.Clear;
finalize(IdMa);
if not Query4.IsEmpty then repeat
setLength(IdMa,Length(IdMa)+1);
IdMa[high(IdMa)]:= Query4.FieldByName('Id').AsString;
ComboBox3.Items.Add(Query4.FieldByName('NazvaMagazuny').AsString);
until Query4.MoveBy(1)<1;
     ComboBox3.ItemIndex:=-1;
     ComboBox3.Text:='';
  Edit1.Text:='';
  Edit2.Text:='';
  Edit3.Text:='';
  Edit4.Text:='';
  Edit5.Text:='';
  Edit6.Text:='';
  Edit7.Text:='';
  MaskEdit1.Text:='';
  MaskEdit2.Text:='';
  MaskEdit3.Text:='';
  Edit1.Enabled:=true;
  Edit2.Enabled:=true;
  Edit3.Enabled:=true;
  Edit4.Enabled:=true;
  Edit5.Enabled:=true;
  Edit6.Enabled:=true;
  Edit7.Enabled:=true;
  MaskEdit1.Enabled:=true;
  MaskEdit2.Enabled:=true;
  MaskEdit3.Enabled:=true;
  BitBtn4.Enabled:=true;
  BitBtn5.Enabled:=true;
  label1.Enabled:=true;
  label2.Enabled:=true;
  label3.Enabled:=true;
  label4.Enabled:=true;
  label5.Enabled:=true;
  label6.Enabled:=true;
  label7.Enabled:=true;
  label8.Enabled:=true;
  label9.Enabled:=true;
  label10.Enabled:=true;
  label11.Enabled:=true;
  BitBtn1.Enabled:=false;
  BitBtn2.Enabled:=false;
  BitBtn3.Enabled:=false;
  BitBtn6.Enabled:=false;
  BitBtn7.Enabled:=false;
  BitBtn8.Enabled:=false;
  BitBtn9.Enabled:=false;
  BitBtn10.Enabled:=false;
  BitBtn11.Enabled:=false;
  combobox1.Enabled:=true;
  combobox2.Enabled:=true;
  combobox3.Enabled:=true;
  combobox4.Enabled:=false;
  combobox5.Enabled:=false;
  GroupBox4.Enabled:=false;
  GroupBox5.Enabled:=false;
  GroupBox6.Enabled:=false;
  label12.Enabled:=false;
  label13.Enabled:=false;
 Self.Tag:=1;
        end;

procedure TForm3.FormActivate(Sender: TObject);
begin
  Query1.Open();
    Edit1.Text:='';
  Edit2.Text:='';
  Edit3.Text:='';
  Edit4.Text:='';
  Edit5.Text:='';
  Edit6.Text:='';
  Edit7.Text:='';
  MaskEdit1.Text:='';
  MaskEdit2.Text:='';
  MaskEdit3.Text:='';
  Edit1.Enabled:=false;
  Edit2.Enabled:=false;
  Edit3.Enabled:=false;
  Edit4.Enabled:=false;
  Edit5.Enabled:=false;
  Edit6.Enabled:=false;
  Edit7.Enabled:=false;
  MaskEdit1.Enabled:=false;
  MaskEdit2.Enabled:=false;
  MaskEdit3.Enabled:=false;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
  label3.Enabled:=false;
  label4.Enabled:=false;
  label5.Enabled:=false;
  label6.Enabled:=false;
  label7.Enabled:=false;
  label8.Enabled:=false;
  label9.Enabled:=false;
  label10.Enabled:=false;
  label11.Enabled:=false;
  combobox1.Enabled:=false;
  combobox2.Enabled:=false;
  combobox3.Enabled:=false;
  end;

procedure TForm3.BitBtn5Click(Sender: TObject);
begin
  Edit1.Text:='';
  Edit2.Text:='';
  Edit3.Text:='';
  Edit4.Text:='';
  Edit5.Text:='';
  Edit6.Text:='';
  Edit7.Text:='';
  MaskEdit1.Text:='';
  MaskEdit2.Text:='';
  MaskEdit3.Text:='';
  combobox1.Text:='';
  combobox2.Text:='';
  combobox3.Text:='';
  Edit1.Enabled:=false;
  Edit2.Enabled:=false;
  Edit3.Enabled:=false;
  Edit4.Enabled:=false;
  Edit5.Enabled:=false;
  Edit6.Enabled:=false;
  Edit7.Enabled:=false;
  MaskEdit1.Enabled:=false;
  MaskEdit2.Enabled:=false;
  MaskEdit3.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
  label3.Enabled:=false;
  label4.Enabled:=false;
  label5.Enabled:=false;
  label6.Enabled:=false;
  label7.Enabled:=false;
  label8.Enabled:=false;
  label9.Enabled:=false;
  label10.Enabled:=false;
  label11.Enabled:=false;
  label12.Enabled:=true;
  label13.Enabled:=true;
  combobox1.Enabled:=false;
  combobox2.Enabled:=false;
  combobox3.Enabled:=false;
  combobox4.Enabled:=true;
  combobox5.Enabled:=true;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  BitBtn3.Enabled:=true;
  BitBtn6.Enabled:=true;
  BitBtn7.Enabled:=true;
  BitBtn8.Enabled:=true;
  BitBtn9.Enabled:=true;
  BitBtn10.Enabled:=true;
  BitBtn11.Enabled:=true;
end;

procedure TForm3.BitBtn4Click(Sender: TObject);
begin
If Form3.Tag=1 then begin
if (Edit1.text='') or (Length(Edit3.Text)<>8) or (Length(Edit4.Text)<>10) or (Edit6.text='')
 or (Edit7.text='') or (Pos('_',MaskEdit1.Text)<>0) or (Pos('_',MaskEdit2.Text)<>0)
or (Pos('_',MaskEdit3.Text)<>0) then
begin
Application.MessageBox('Введіть коректні дані','Помилка',MB_OK+MB_ICONWARNING);
exit;
end;
 Query5.SQL.Clear;
 Query5.SQL.Add('INSERT INTO spivrobitnuku (PIB,DataNarodgennya,PasportniDani,IPN,DataVlash,Adresa,Telefon,IdViddily,IdPosadu,Oklad,IdMagazuny) values ('+QuotedStr(edit1.text)+','''+MaskEdit1.Text+''','+QuotedStr(edit3.text)+','+QuotedStr(edit4.text)+','''+MaskEdit2.Text+''','+QuotedStr(edit6.text)+','''+MaskEdit3.Text+''','''+IDVi[ComboBox1.ItemIndex]+''','''+IDPo[ComboBox2.ItemIndex]+''','+QuotedStr(edit7.text)+','''+IDMa[ComboBox3.ItemIndex]+''')');
 Query5.ExecSQL;
 Query1.Close;
 Query1.Open; end;
If Form3.Tag=2 then
begin
 Query5.SQL.Clear;
 Query5.SQL.Add('UPDATE spivrobitnuku Set PIB='+QuotedStr(Edit1.Text)+', DataNarodgennya='''+MaskEdit1.Text+''', PasportniDani='+QuotedStr(Edit3.Text)+', IPN='+QuotedStr(Edit4.Text)+', DataVlash='''+MaskEdit2.Text+''', Adresa='+QuotedStr(Edit6.Text)+', Telefon='''+MaskEdit3.Text+''', IdViddily='+IDVi[ComboBox1.ItemIndex]+', IdPosadu='+IDPo[ComboBox2.ItemIndex]+', Oklad='+QuotedStr(Edit7.Text)+', IdMagazuny='+IdMa[ComboBox3.ItemIndex]+' WHERE spivrobitnuku.Id='+Query1.FieldByName('Id').AsString);
 Query5.ExecSQL;
 Query1.Close;
 Query1.Open;
 end;

  Edit1.Text:='';
  Edit2.Text:='';
  Edit3.Text:='';
  Edit4.Text:='';
  Edit5.Text:='';
  Edit6.Text:='';
  Edit7.Text:='';
  MaskEdit1.Text:='';
  MaskEdit2.Text:='';
  MaskEdit3.Text:='';
  combobox1.Text:='';
  combobox2.Text:='';
  combobox3.Text:='';
  Edit1.Enabled:=false;
  Edit2.Enabled:=false;
  Edit3.Enabled:=false;
  Edit4.Enabled:=false;
  Edit5.Enabled:=false;
  Edit6.Enabled:=false;
  Edit7.Enabled:=false;
  MaskEdit1.Enabled:=false;
  MaskEdit2.Enabled:=false;
  MaskEdit3.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
  label3.Enabled:=false;
  label4.Enabled:=false;
  label5.Enabled:=false;
  label6.Enabled:=false;
  label7.Enabled:=false;
  label8.Enabled:=false;
  label9.Enabled:=false;
  label10.Enabled:=false;
  label11.Enabled:=false;
  label12.Enabled:=true;
  label13.Enabled:=true;
  combobox1.Enabled:=false;
  combobox2.Enabled:=false;
  combobox3.Enabled:=false;
  combobox4.Enabled:=true;
  combobox5.Enabled:=true;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  BitBtn3.Enabled:=true;
  BitBtn6.Enabled:=true;
  BitBtn7.Enabled:=true;
  BitBtn8.Enabled:=true;
  BitBtn9.Enabled:=true;
  BitBtn10.Enabled:=true;
  BitBtn11.Enabled:=true;
  DbGrid1.SetFocus();
   end;

procedure TForm3.BitBtn2Click(Sender: TObject);
var i,n,k,j:integer;
s:string;
begin
Query2.SQL.Clear;
Query2.SQL.Add('SELECT* FROM viddilu');
Query2.Active:=False;
Query2.Active:=True;
ComboBox1.Items.Clear;
finalize(IdVi);
if not Query2.IsEmpty then repeat
setLength(IdVi,Length(IdVi)+1);
IdVi[high(IdVi)]:= Query2.FieldByName('Id').AsString;
ComboBox1.Items.Add(Query2.FieldByName('NazvaViddily').AsString);
until Query2.MoveBy(1)<1;
     ComboBox1.ItemIndex:=-1;
     ComboBox1.Text:='';

Query3.SQL.Clear;
Query3.SQL.Add('SELECT* FROM posadu');
Query3.Active:=False;
Query3.Active:=True;
ComboBox2.Items.Clear;
finalize(IdPo);
if not Query3.IsEmpty then repeat
setLength(IdPo,Length(IdPo)+1);
IdPo[high(IdPo)]:= Query3.FieldByName('Id').AsString;
ComboBox2.Items.Add(Query3.FieldByName('NazvaPosadu').AsString);
until Query3.MoveBy(1)<1;
     ComboBox2.ItemIndex:=-1;
     ComboBox2.Text:='';

Query4.SQL.Clear;
Query4.SQL.Add('SELECT* FROM magazunu');
Query4.Active:=False;
Query4.Active:=True;
ComboBox3.Items.Clear;
finalize(IdMa);
if not Query4.IsEmpty then repeat
setLength(IdMa,Length(IdMa)+1);
IdMa[high(IdMa)]:= Query4.FieldByName('Id').AsString;
ComboBox3.Items.Add(Query4.FieldByName('NazvaMagazuny').AsString);
until Query4.MoveBy(1)<1;
     ComboBox3.ItemIndex:=-1;
     ComboBox3.Text:='';

for i:=1 to Length (IdVi) do
  for n:=1 to Length (IdPo) do
    for k:=1 to Length (IdMa) do begin
if IdVi[i-1]=Form3.Query1IdViddily.AsString
then begin
  ComboBox1.ItemIndex:=i-1; end;
if IdPo[n-1]=Form3.Query1IdPosadu.AsString
then begin
  ComboBox2.ItemIndex:=n-1; end;
If IdMa[k-1]=Form3.Query1IdMagazuny.AsString
then begin
  ComboBox3.ItemIndex:=k-1; end;  end;
Edit1.Text:=Query1.FieldByName('PIB').AsString;
Edit2.Text:=Query1.FieldByName('DataNarodgennya').AsString;
Edit3.Text:=Query1.FieldByName('PasportniDani').AsString;
Edit4.Text:=Query1.FieldByName('IPN').AsString;
Edit5.Text:=Query1.FieldByName('DataVlash').AsString;
Edit6.Text:=Query1.FieldByName('Adresa').AsString;
Edit7.Text:=Form3.Query1.FieldByName('Oklad').AsString;
MaskEdit1.Text:=FormatDateTime('YYYY-MM-DD',StrToDate(Edit2.Text));
MaskEdit2.Text:=FormatDateTime('YYYY-MM-DD',StrToDate(Edit5.Text));
MaskEdit3.Text:=Query1.FieldByName('Telefon').AsString;
IDVi[ComboBox1.ItemIndex]:=Form3.Query1.FieldByName('IdViddily').AsString;
IDPo[ComboBox2.ItemIndex]:=Form3.Query1.FieldByName('IdPosadu').AsString;
IDma[ComboBox3.ItemIndex]:=Form3.Query1.FieldByName('IdMagazuny').AsString;
s:=Edit7.Text;
  for j:=1 to Length(s) do begin
    If s[j]=',' then s[j]:='.';
    end;
   Edit7.Text:=s;

 Edit1.Enabled:=true;
  Edit2.Enabled:=true;
  Edit3.Enabled:=true;
  Edit4.Enabled:=true;
  Edit5.Enabled:=true;
  Edit6.Enabled:=true;
  Edit7.Enabled:=true;
  MaskEdit1.Enabled:=true;
  MaskEdit2.Enabled:=true;
  MaskEdit3.Enabled:=true;
  BitBtn4.Enabled:=true;
  BitBtn5.Enabled:=true;
  label1.Enabled:=true;
  label2.Enabled:=true;
  label3.Enabled:=true;
  label4.Enabled:=true;
  label5.Enabled:=true;
  label6.Enabled:=true;
  label7.Enabled:=true;
  label8.Enabled:=true;
  label9.Enabled:=true;
  label10.Enabled:=true;
  label11.Enabled:=true;
  BitBtn1.Enabled:=false;
  BitBtn2.Enabled:=false;
  BitBtn3.Enabled:=false;
  BitBtn6.Enabled:=false;
  BitBtn7.Enabled:=false;
  BitBtn8.Enabled:=false;
  BitBtn9.Enabled:=false;
  BitBtn10.Enabled:=false;
  BitBtn11.Enabled:=false;
  combobox1.Enabled:=true;
  combobox2.Enabled:=true;
  combobox3.Enabled:=true;
  combobox4.Enabled:=false;
  combobox5.Enabled:=false;
  GroupBox4.Enabled:=false;
  GroupBox5.Enabled:=false;
  GroupBox6.Enabled:=false;
  label12.Enabled:=false;
  label13.Enabled:=false;
 Self.Tag:=2;
        end;

procedure TForm3.Edit4KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in['0' .. '9', #8] ) then Key:=Chr(0);
end;

procedure TForm3.Edit7KeyPress(Sender: TObject; var Key: Char);
begin
         case Key of
    '0' .. '9', #8 : ;
     '.', ',': begin
      if  Key = ',' then Key:='.';
     if (Pos('.',Edit7.Text)<>0) then  Key:=Chr(0);
          end; else Key:=Chr(0);
   end;
end;

procedure TForm3.Filter;
begin
Query1.Filter:='DataVlash>='''+(DateToStr(DateTimePicker1.Date))+''''+' and DataVlash<='''+(DateToStr(DateTimePicker2.Date))+'''';
Query1.Filtered:= True;
end;

procedure TForm3.DateTimePicker1CloseUp(Sender: TObject);
begin
Filter;
end;

procedure TForm3.DateTimePicker2CloseUp(Sender: TObject);
begin
Filter;
end;

procedure TForm3.BitBtn6Click(Sender: TObject);
begin
Query1.Filtered:=False;
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
ComboBox4.Items.Clear;
    ComboBox4.Items.Add('-Всі записи-');
    Query6.First();
    i:=0;
    if not Query6.IsEmpty then
    repeat
      SetLength(mas, length(mas)+1);
      mas[i]:=Query6NazvaViddily.AsString;
      ComboBox4.Items.Add(Query6NazvaViddily.AsString);
      inc(i);
    until Query6.MoveBy(1)<1;
    ComboBox4.ItemIndex:=0;

    ComboBox5.Items.Clear;
    ComboBox5.Items.Add('-Всі записи-');
    Query7.First();
    j:=0;
    if not Query7.IsEmpty then
    repeat
      SetLength(masb, length(masb)+1);
      masb[j]:=Query7NazvaMagazuny.AsString;
      ComboBox5.Items.Add(Query7NazvaMagazuny.AsString);
      inc(j);
    until Query7.MoveBy(1)<1;
    ComboBox5.ItemIndex:=0;
end;

procedure TForm3.ComboBox4CloseUp(Sender: TObject);
begin
 i:=Combobox4.ItemIndex;
    if i>0 then
      begin
        Query1.Filter:='NazvaViddily='+QuotedStr(mas[i-1]);
        Query1.Filtered:=true;
     end
     else
        Query1.Filtered:=false;
end;

procedure TForm3.ComboBox5CloseUp(Sender: TObject);
begin
   j:=Combobox5.ItemIndex;
    if j>0 then
      begin
        Query1.Filter:='NazvaMagazuny='+QuotedStr(masb[j-1]);
        Query1.Filtered:=true;
     end
     else
        Query1.Filtered:=false;
end;

procedure TForm3.BitBtn10Click(Sender: TObject);
var
 XLApp,Sheet,Colum:Variant;
 index,index2,i,s:Integer;
begin
try
 XLApp:= CreateOleObject('Excel.Application');
 TQuery(DBGrid1.DataSource.DataSet).DisableControls;
//Замість DBGrid1 буде назва Вашого DBGrid'a, в якому відображаються дані
 XLApp.Visible:=False;
 XLApp.Workbooks.Add(-4167);
 XLApp.Workbooks[1].WorkSheets[1].Name:=Self.Caption;
 Colum:=XLApp.Workbooks[1].WorkSheets[Self.Caption].Columns;
 index:=1;
 Colum:=XLApp.Workbooks[1].WorkSheets[Self.Caption].Rows;
 Colum.Rows[1].Font.Bold:=true;
 Colum.Rows[1].Font.Size:=10;
 Sheet:=XLApp.Workbooks[1].WorkSheets[Self.Caption];
 for I := 1 to  TQuery(DBGrid1.DataSource.DataSet).FieldCount do
   if TQuery(DBGrid1.DataSource.DataSet).Fields[i-1].Visible then
   begin
   if TQuery(DBGrid1.DataSource.DataSet).Fields[i-1].Size>0 then Colum.Columns[index].ColumnWidth:=TQuery(DBGrid1.DataSource.DataSet).Fields[i-1].Size
   else Colum.Columns[index].ColumnWidth:=12;
   Sheet.Cells[1,index].Value:=TQuery(DBGrid1.DataSource.DataSet).Fields[i-1].DisplayLabel;
   Inc(index);
   end;
TQuery(DBGrid1.DataSource.DataSet).First;
 for i:=2 to TQuery(DBGrid1.DataSource.DataSet).RecordCount+1 do
  begin
  index2:=1;
   for s := 0 to  TQuery(DBGrid1.DataSource.DataSet).FieldCount-1 do
   if TQuery(DBGrid1.DataSource.DataSet).Fields[s].Visible then
   begin
   Sheet.Cells[i,index2].Value:=TQuery(DBGrid1.DataSource.DataSet).Fields[s].AsString;
   Inc(index2);
   end;
   TQuery(DBGrid1.DataSource.DataSet).Next;
  end;
XLApp.Range[XLApp.Cells[1, 1], XLApp.Cells[TQuery(DBGrid1.DataSource.DataSet).RecordCount+1,index-1]].Select;
XLApp.Selection.Borders.LineStyle:=1;
XLApp.Selection.WrapText:=True;
XLApp.Range['A1'].Select;
TQuery(DBGrid1.DataSource.DataSet).EnableControls;
XLApp.Visible:=true;
except
Application.MessageBox(PChar('Microsoft Ecxel не інстальований, або виникла інша помилка.'), 'Помилка!!!', MB_ICONWARNING+MB_OK);
  if VarIsEmpty(XLApp) = false then
  begin
    XLApp.Quit;
    XLApp := 0;
  end;
exit;
end;
end;

procedure TForm3.BitBtn7Click(Sender: TObject);
var s:string;
begin
 s:=DBGrid1.SelectedField.FieldName;
 Form5.ShowModal;
 if Form5.ModalResult=mrCancel then exit;
 Query1.close;
 if Query1.SQL.Count<3 then Query1.SQL.add(' ');
 Query1.SQL[2]:=Query1.SQL[2]+' AND '+s+'='''+Form5.Edit1.Text+'''';
Form3.Query1.Open;
end;

procedure TForm3.BitBtn8Click(Sender: TObject);
var s:string;
begin
 s:=DBGrid1.SelectedField.FieldName;
 Form6.ShowModal;
 if Form6.ModalResult=mrCancel then exit;
 Query1.close;
 if Query1.SQL.Count<3 then Query1.SQL.add(' ');
 Query1.SQL[2]:=Query1.SQL[2]+' AND '+s+' LIKE ''%'+Form6.Edit1.Text+'%''';
 Form3.Query1.Open;
end;

procedure TForm3.BitBtn9Click(Sender: TObject);
begin
 Query1.Close;
  Query1.SQL[2]:='WHERE spivrobitnuku.IdPosadu = posadu.Id AND spivrobitnuku.IdViddily = viddilu.Id AND spivrobitnuku.Idmagazuny=magazunu.id';
  Query1.Open;
end;

procedure TForm3.BitBtn11Click(Sender: TObject);
begin
 close;
end;

procedure TForm3.BitBtn3Click(Sender: TObject);
var
 uc1,uc2,uc3,uc4:integer;
begin
 Query2.Active:=False;
 Query2.SQL.Clear;
 Query2.SQL.Add(FORMAT('SELECT COUNT(*) From magazunu where magazunu.IdSpivrobitnuka=%s;',[Query1.FieldByName('Id').AsString]));
 Query2.Active:=True;
 uc1:=Query2.FieldByName('COUNT(*)').AsInteger;

 Query3.Active:=False;
 Query3.SQL.Clear;
 Query3.SQL.Add(FORMAT('SELECT COUNT(*) From nadhodgennya where nadhodgennya.IdSpivrobitnuka=%s;',[Query1.FieldByName('Id').AsString]));
 Query3.Active:=True;
 uc2:=Query3.FieldByName('COUNT(*)').AsInteger;

 Query4.Active:=False;
 Query4.SQL.Clear;
 Query4.SQL.Add(FORMAT('SELECT COUNT(*) From rozpodil where rozpodil.IdSpivrobitnuka=%s;',[Query1.FieldByName('Id').AsString]));
 Query4.Active:=True;
 uc3:=Query4.FieldByName('COUNT(*)').AsInteger;

 Query5.Active:=False;
 Query5.SQL.Clear;
 Query5.SQL.Add(FORMAT('SELECT COUNT(*) From vurychka where vurychka.IdSpivrobitnuka=%s;',[Query1.FieldByName('Id').AsString]));
 Query5.Active:=True;
 uc4:=Query5.FieldByName('COUNT(*)').AsInteger;

if (uc1=0) and (uc2=0) and (uc3=0) then
 Begin   if
 Application.MessageBox('Ви дійсно хочете видалити запис?','Увага!!!',MB_YesNo+MB_ICONQUESTION)=MrYes then
 begin
 Query2.Active:=False;
 Query2.SQL.Clear;
 Query2.SQL.Add('DELETE FROM spivrobitnuku WHERE spivrobitnuku.Id='+Query1.FieldByName('Id').AsString+';');
 Query2.ExecSQL;
 Query1.Active:=False;
 Query1.Active:=True;
 end;
 end else
  MessageBoxA(Handle,'Неможливо видалити, запис зустрічається в інших таблицях!','Помилка!',MB_ICONERROR);
end;


end.
