unit u_editor;
{Version 1.1}
{$Warnings off}
{$Hints on}
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, DB, DBTables, CheckLst, ExtCtrls, Grids;

type
  TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    GroupBox2: TGroupBox;
    Panel1: TPanel;
    Label3: TLabel;
    ComboBox2: TComboBox;
    Label4: TLabel;
    ComboBox3: TComboBox;
    Edit2: TEdit;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button1: TButton;
    Panel2: TPanel;
    Label1: TLabel;
    GroupBox1: TGroupBox;
    GroupBox3: TGroupBox;
    Label2: TLabel;
    ComboBox1: TComboBox;
    Edit1: TEdit;
    Button2: TButton;
    Button3: TButton;
    Button7: TButton;
    Label5: TLabel;
    ComboBox4: TComboBox;
    Edit3: TEdit;
    Label6: TLabel;
    Edit7: TEdit;
    Edit6: TEdit;
    Label9: TLabel;
    Label8: TLabel;
    Edit5: TEdit;
    Edit4: TEdit;
    Label7: TLabel;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    Label10: TLabel;
    Edit8: TEdit;
    TabSheet3: TTabSheet;
    Panel3: TPanel;
    StringGrid1: TStringGrid;
    Button11: TButton;
    Edit10: TEdit;
    ComboBox5: TComboBox;
    StringGrid2: TStringGrid;
    procedure ComboBox2DropDown(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ComboBox3DropDown(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure ComboBox2Select(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure ComboBox3Select(Sender: TObject);
    procedure CheckListBox2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ComboBox1DropDown(Sender: TObject);
    procedure ComboBox1Select(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure ComboBox4DropDown(Sender: TObject);
    procedure ComboBox4Select(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TTreeField = Class
    dep_ID, dep_Name,
    kaf_ID, kaf_Name : String;

    value : Single;
    Checked : boolean;
    Item : Array of record
      _Used    : Boolean;
      _Name    : String;
      _IDString,
      _Weight  : String;
      _RText   : Array [0..3] of String;
      _ID      : Integer;
    end;
    Function Add(zName:String): Integer;
    Function FindByID(zID: Integer): integer;
    Procedure Clear;
  end;


  PTable = ^TTable;

const
  db_dep = 0;
  db_kaf = 1;
  db_std = 2;
  db_chk = 3;
  db_KSC = 4;
  db_stdchk = 5;

var
  Form1: TForm1;

  Table  : Array [0..5] of TTable;
  APPDir : String;
  CurSec,
  FileName : String;

implementation
var
  TMP_LIST : Array [0..5] of TTreeField;
const
 c : char = '''';
 l_dep     = 0;
 l_kaf     = 1;
 l_kaf_std = 2;
 l_std     = 3;
 l_chk     = 4;
 l_stdchk  = 5;

{$R *.dfm}

{------------------------------------------------------------------------------}
Procedure CheckCreateTable(TableName, addFields: string);
begin
  if FileExists(TableName) then exit;
    with TQuery.Create(form1) do
        begin
           SQL.Clear;
           SQL.Add('create table "'+TableName+'" ( '+addFields+' );');
          ExecSQL;
          Close;
          Free;
        end;
end;

 Function TTreeField.Add(zName:String) : Integer;
 var i,i2 : Integer;
 begin
  I := Length(Item);
    SetLength(Item, I + 1 );
         with Item[ i ] do begin
          _Used    := true;
          _Name    := zName;
          for i2 := 0 to High(_RText) do
          _RText[i2] := '';
          _ID      := 0;
         end;
    result := i;
 end;

 Function TTreeField.FindByID(zID: Integer): integer;
 var i : integer;
 begin
    for i := 0 to high(Item) do
      if item[i]._Id = zid then
       begin
        result:=i;
        exit;
       end;
        result :=-1;
 end;

 Procedure TTreeField.Clear;
 begin
  SetLength(Item,0);
 end;

{------------------------------------------------------------------------------}

 Procedure Filter(Tbl:PTable; S:String);
 begin
    tbl.Filtered := false;
    tbl.Filter   := S;
    tbl.Filtered := true;
    if not tbl.eof then
    Tbl.First;
 end;

 Procedure OpenBD(Tbl: Array of PTable);
 var i : Integer;
 begin
   for i := 0 to high(tbl) do
   begin
    Tbl[i].Open;
    Tbl[i].First;
   end;

 end;

 Procedure CloseBD(Tbl: Array of PTable);
 var i : Integer;
 begin
   for i := 0 to high(tbl) do
   begin
    Tbl[i].Filtered :=false;
    Tbl[i].First;
    Tbl[i].Close;
   end;
 end;




procedure TForm1.ComboBox2DropDown(Sender: TObject);
var i : integer;
begin
  table[ db_dep ].Open;
  table[ db_dep ].First;
  table[ db_dep ].Filtered :=false;
  TCombobox(Sender).Items.Clear;

  TMP_List [ L_Dep ].Clear;

  while not table[ db_dep ].Eof do
  begin
   i := TMP_List [ L_Dep ].Add(                 table[ db_dep ].FieldByName('Dep_Name').asstring );
        TMP_List [ L_Dep ].Item[i]._IDString := table[ db_dep ].FieldByName('Dep_ID').asstring;
   TCombobox(Sender).Items.Add( TMP_List [ L_Dep ].Item[i]._Name );
   table[ db_dep ].Next;
  end;
  if TCombobox(Sender).Items.Count > 0 then
     TCombobox(Sender).ItemIndex := 0 ;
  table[ db_dep ].Close;
end;

procedure TForm1.FormCreate(Sender: TObject);
VAR
I : integer;
begin
for i := 0 to high( Table ) do
  Table[i] := TTable.Create(form1);

  Appdir   := ExtractFilepath( Application.ExeName ) + '\WorkDir\';


  table[ db_dep ].TableName := AppDir + 'dep1.DB';
  table[ db_kaf ].TableName := AppDir + 'kaf2.DB';
  table[ db_std ].TableName := AppDir + 'standard3.DB';
  table[ db_chk ].TableName := AppDir + 'check4.DB';
  table[ db_KSC ].TableName := AppDir + 'KSC5.DB';
  table[ db_stdchk ].TableName := AppDir + 'std_check_log.db';

  for i := 0 to high( TMP_List ) do
  TMP_List[i] := TTreeField.create;
end;

procedure TForm1.ComboBox3DropDown(Sender: TObject);
var i : integer;
begin
if (ComboBox2.itemindex = -1)
   then exit;

  table[ db_kaf ].Open;
  table[ db_kaf ].Filtered :=false;
  Filter(@table[ db_kaf ],'Kaf_Dep_id = '+C+ TMP_List[ L_Dep ].item[ ComboBox2.itemindex ]._IDString +C);
  TCombobox(Sender).Items.Clear;

  TMP_List[ L_KAF ].Clear;
  while not table[ db_kaf ].Eof do
  begin
  i:= TMP_List[ L_KAF ].Add(                 table[ db_kaf ].FieldByName('Kaf_Name').asstring );
      TMP_List[ L_KAF ].Item[i]._IDString := table[ db_kaf ].FieldByName('Kaf_ID').asstring;

   TCombobox(Sender).Items.Add( TMP_List[ L_KAF ].Item[i]._Name );
   table[ db_kaf ].Next;
  end;
  if TCombobox(Sender).Items.Count > 0 then
      begin
        TCombobox(Sender).ItemIndex := 0 ;
        Edit2.Text := TMP_List[ L_KAF ].Item[0]._Name;
      end;

  table[ db_kaf ].Close;

end;

procedure TForm1.Button4Click(Sender: TObject);
begin
if (ComboBox2.itemindex = -1) then exit;

  table[ db_kaf ].ReadOnly :=false;
  table[ db_kaf ].Open;
  table[ db_kaf ].Append;
  table[ db_kaf ].FieldByName('Kaf_Name').asstring   := Edit2.Text;
  table[ db_kaf ].FieldByName('Kaf_Dep_Id').asstring := TMP_List[ L_Dep ].item[ ComboBox2.itemindex ]._IDString;
  table[ db_kaf ].post;

  table[ db_kaf ].Close;
  table[ db_kaf ].ReadOnly :=true;

end;

procedure TForm1.ComboBox2Select(Sender: TObject);
begin
   ComboBox3DropDown(Combobox3);
  Combobox3Select( Combobox3 );
end;

procedure TForm1.Button6Click(Sender: TObject);
var id: integer;
begin
if (ComboBox2.itemindex = -1)
or (ComboBox3.itemindex = -1)
   then exit;

  table[ db_kaf ].ReadOnly :=false;
  OpenBD([ @table[ db_kaf ] ]);
  Filter(@table[ db_kaf ],'Kaf_ID = '+C+TMP_List[ L_Kaf ].item[ ComboBox3.itemindex ]._IDString+C);

  table[ db_kaf ].Edit;
  table[ db_kaf ].FieldByName('Kaf_Name').asstring   := Edit2.Text;
  table[ db_kaf ].FieldByName('Kaf_Dep_Id').asstring := TMP_List[ L_Dep ].item[ ComboBox2.itemindex ]._IDString;

  table[ db_kaf ].post;

  CloseBD([ @table[ db_kaf ] ]);
  table[ db_kaf ].ReadOnly :=true;
  table[ db_kaf ].Filtered := false;
  id := Combobox3.ItemIndex;
  ComboBox3DropDown(Combobox3);
  Combobox3.ItemIndex := id;
  Edit2.Text := Combobox3.Items[ ComboBox3.itemindex ];
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
if (ComboBox2.itemindex = -1)
or (ComboBox3.itemindex = -1)
   then exit;

  table[ db_kaf ].ReadOnly :=false;
  OpenBD([ @table[ db_kaf ] ]);
  Filter(  @table[ db_kaf ],'Kaf_ID = '+C+TMP_List[ L_Kaf ].item[ ComboBox3.itemindex ]._IDString+C);
  if not table[ db_kaf ].eof then
         table[ db_kaf ].Delete else
     begin
        MessageBox(0,Pchar( 'Не найдена кафедра : ' +TMP_List[ L_Kaf ].Item[ ComboBox3.itemindex ]._Name
                   +#13+#10+'с идентификатором : '  +TMP_List[ L_Kaf ].Item[ ComboBox3.itemindex ]._IDString
                           ),nil,MB_IconERROR);
     end;
  CloseBD([ @table[ db_kaf ] ]);
  ComboBox3DropDown(Combobox3);
end;

procedure TForm1.ComboBox3Select(Sender: TObject);
var
  I,J,W : Integer;
begin
if combobox3.ItemIndex = -1 then exit;

  OpenBD([@table[ db_dep ],@table[ db_kaf ],@table[ db_std ] , @table[ db_ksc ] ]);

            table[ db_std ].Filtered :=false;

  StringGrid2.RowCount :=2;
  TMP_List[ l_kaf_std ].Clear;
  TMP_List[ l_kaf_std ].dep_Name := TMP_List[ L_dep ].item[ ComboBox2.itemindex ]._Name;
  TMP_List[ l_kaf_std ].dep_id   := TMP_List[ L_dep ].item[ ComboBox2.itemindex ]._IDString;
  TMP_List[ l_kaf_std ].kaf_Name := TMP_List[ L_Kaf ].item[ ComboBox3.itemindex ]._Name;
  TMP_List[ l_kaf_std ].kaf_id   := TMP_List[ L_Kaf ].item[ ComboBox3.itemindex ]._IDString;

  StringGrid2.ColWidths[0] := 24;
  StringGrid2.ColWidths[1] := 30;

  while not table[ db_std ].Eof do
  begin
    Filter(@table[ db_ksc ],' KSC_Kaf_ID = '+C+ TMP_List[ L_Kaf ].item[ ComboBox3.itemindex ]._IDString +C
                          + 'and KSC_Standard_ID = '+C+table[ db_std ].FieldByName('Standard_id').asstring+C );

    i:= TMP_List[ l_kaf_std ].Add( table[ db_std ].FieldByName('Standard_Name').asstring );
        TMP_List[ l_kaf_std ].item[i]._IDString := table[ db_std ].FieldByName('Standard_id').asstring;
        TMP_List[ l_kaf_std ].item[i]._Weight   := table[ db_ksc ].FieldByName('KSC_Standard_Weight').asstring;
     With StringGrid2 do begin
      j := RowCount-1;
      W := Canvas.TextWidth(TMP_List[ l_kaf_std ].item[i]._Name);
           Cells[2, j]  := TMP_List[ l_kaf_std ].item[i]._Name ;
           Cells[1, j]  := TMP_List[ l_kaf_std ].item[i]._Weight ;
           Cells[0, j]  := '0';
        if ColWidths[2] < W then ColWidths[2] := W;
//     CheckListBox2.Checked[ CheckListBox2.Count -1 ] := False;
     end;

     table[ db_ksc ].First;
      while not table[ db_ksc ].Eof do
      begin
         if table[ db_std ].FieldByName('Standard_ID').asstring =
            table[ db_ksc ].FieldByName('KSC_Standard_ID').asstring then
            begin
               StringGrid2.Cells[0, j]  := '1';
//              CheckListBox2.Checked[ CheckListBox2.Count -1 ] := true;
              break;
            end;
        table[ db_ksc ].Next;


      end;

//     TMP_List[ l_kaf_std ].item[i]._Used := CheckListBox2.Checked[ CheckListBox2.Count -1 ];

   table[ db_std ].Next;
   if not table[ db_std ].Eof then with StringGrid2 do RowCount := RowCount+1;
  end;


  CloseBD([@table[ db_dep ],@table[ db_kaf ],@table[ db_std ] , @table[ db_ksc ] ]);
  Edit2.Text := Combobox3.Items[ ComboBox3.itemindex ];
end;

procedure TForm1.CheckListBox2Click(Sender: TObject);
begin//
end;

procedure TForm1.Button1Click(Sender: TObject);
var i : integer;
begin
if (ComboBox2.itemindex = -1)
or (ComboBox3.itemindex = -1)
   then exit;



  table[ db_ksc ].ReadOnly :=false;
  OpenBD([ @Table[ db_ksc ] ]);
  Filter(  @Table[ db_ksc ],'KSC_KAF_ID = '+C+TMP_List[ l_kaf_std ].kaf_ID  +C);

  while not table[ db_ksc ].eof do
            table[ db_ksc ].Delete;

  for i := 0 to High(TMP_List[ l_kaf_std ].item) do
  With StringGrid2 do
//  if CheckListBox2.Checked[i] then
  if Cells[0,i] = '1' then
  begin
    table[ db_ksc ].Append;
    table[ db_ksc ].FieldByName('KSC_Kaf_ID').asstring          := TMP_List[ l_kaf_std ].kaf_ID;
    table[ db_ksc ].FieldByName('KSC_Standard_ID').asstring     := TMP_List[ l_kaf_std ].item[i]._IDString;
    table[ db_ksc ].FieldByName('KSC_Standard_Weight').asstring := Cells[1,i];

    table[ db_ksc ].post;
  end;

  CloseBD([ @table[ db_ksc ] ]);
  table[ db_ksc ].ReadOnly :=true;
end;

procedure TForm1.ComboBox1DropDown(Sender: TObject);
var i : integer;
begin
  OpenBD( [ @table[ db_std ] ]);
  TCombobox(Sender).Items.Clear;
  TMP_List[ l_std ].Clear;

  while not table[ db_std ].Eof do
  begin
   i := TMP_List[ l_std ].Add( table[ db_std ].FieldByName('Standard_Name').asstring );
        TMP_List[ l_std ].Item[i]._IDString := table[ db_std ].FieldByName('Standard_id').asstring;
        TMP_List[ l_std ].Item[i]._RText[0] := table[ db_std ].FieldByName('Standard_ratios').asstring;
        TMP_List[ l_std ].Item[i]._RText[1] := table[ db_std ].FieldByName('Standard_percent').asstring;
        TMP_List[ l_std ].Item[i]._RText[2] := table[ db_std ].FieldByName('Standard_value').asstring;
        TMP_List[ l_std ].Item[i]._RText[3] := table[ db_std ].FieldByName('Standard_RepName').asstring;
//        TMP_List[ l_std ].Item[i]._RText[4] := table[ db_std ].FieldByName('Standard_Weight' ).asstring;

        TCombobox(Sender).Items.Add( TMP_List[ l_std ].Item[i]._Name );
   table[ db_std ].Next;
  end;
  if TCombobox(Sender).Items.Count > 0 then
  begin
     TCombobox(Sender).ItemIndex := 0 ;
     Edit1.Text := TMP_List[ l_std ].Item[0]._Name;
     Edit4.Text := TMP_List[ l_std ].Item[0]._RText[0];
     Edit5.Text := TMP_List[ l_std ].Item[0]._RText[1];
     Edit6.Text := TMP_List[ l_std ].Item[0]._RText[2];
     Edit8.Text := TMP_List[ l_std ].Item[0]._RText[3];
//     Edit9.Text := TMP_List[ l_std ].Item[0]._RText[4];
  end;
  CloseBD( [ @table[ db_std ] ]);

end;

procedure TForm1.ComboBox1Select(Sender: TObject);
var i : integer;
begin
  if TCombobox(Sender).ItemIndex = -1 then exit;
  i := TCombobox(Sender).ItemIndex;
     Edit1.Text := TMP_List[ l_std ].Item[i]._Name;
     Edit4.Text := TMP_List[ l_std ].Item[i]._RText[0];
     Edit5.Text := TMP_List[ l_std ].Item[i]._RText[1];
     Edit6.Text := TMP_List[ l_std ].Item[i]._RText[2];
     Edit8.Text := TMP_List[ l_std ].Item[i]._RText[3];
//     Edit9.Text := TMP_List[ l_std ].Item[i]._RText[4];
end;

procedure TForm1.Button7Click(Sender: TObject);
var id: integer;
begin
if (ComboBox1.itemindex = -1) then exit;

  table[ db_std ].ReadOnly :=false;
  OpenBD([@table[ db_std ]]);
  Filter(@table[ db_std ],'Standard_ID = '+C+ TMP_List[ l_std ].Item[ ComboBox1.itemindex ]._IDString +C);

  table[ db_std ].Edit;
  table[ db_std ].FieldByName('Standard_Name'   ).asstring   := Edit1.Text;
  table[ db_std ].FieldByName('Standard_ratios' ).asstring   := Edit4.Text;
  table[ db_std ].FieldByName('Standard_percent').asstring   := Edit5.Text;
  table[ db_std ].FieldByName('Standard_value'  ).asstring   := Edit6.Text;
  table[ db_std ].FieldByName('Standard_RepName').asstring   := Edit8.Text;
//  table[ db_std ].FieldByName('Standard_Weight' ).asstring   := Edit9.Text;

  table[ db_std ].post;

  CloseBD([@table[ db_std ]]);
  table[ db_std ].ReadOnly :=true;
  table[ db_std ].Filtered := false;
  id := Combobox1.ItemIndex;
  ComboBox1DropDown(Combobox1);
  Combobox1.ItemIndex := id;
  ComboBox1Select  (Combobox1);
  Edit1.Text := Combobox1.Items[ ComboBox1.itemindex ];

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  table[ db_std ].ReadOnly :=false;
  table[ db_std ].Filtered :=false;
  OpenBD([@table[ db_std ]]);
  table[ db_std ].Append;
    table[ db_std ].FieldByName('Standard_Name'   ).asstring   := Edit1.Text;
    table[ db_std ].FieldByName('Standard_ratios' ).asstring   := Edit4.Text;
    table[ db_std ].FieldByName('Standard_percent').asstring   := Edit5.Text;
    table[ db_std ].FieldByName('Standard_value'  ).asstring   := Edit6.Text;
    table[ db_std ].FieldByName('Standard_RepName').asstring   := Edit8.Text;
//    table[ db_std ].FieldByName('Standard_Weight' ).asstring   := Edit9.Text;
  table[ db_std ].post;

  CloseBD([@table[ db_std ]]);
  table[ db_std ].ReadOnly := true;
  table[ db_std ].Filtered := false;
  ComboBox1DropDown(Combobox1);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
if (ComboBox1.itemindex = -1) then exit;

  table[ db_std ].ReadOnly :=false;
  OpenBD([@table[ db_std ],@table[ db_chk ]]);
  Filter(@table[ db_std ],'Standard_ID = '      +C+TMP_List[ l_std ].Item[ ComboBox1.itemindex ]._IDString+C);
  Filter(@table[ db_chk ],'Check_Standard_ID = '+C+TMP_List[ l_std ].Item[ ComboBox1.itemindex ]._IDString+C);
  if table[ db_chk ].eof then
  begin
     if not table[ db_std ].eof then
            table[ db_std ].Delete else
             begin
                MessageBox(0,Pchar( 'Не найден стандарт : ' +TMP_List[ l_std ].Item[ ComboBox1.itemindex ]._Name
                           +#13+#10+'с идентификатором : '  +TMP_List[ l_std ].Item[ ComboBox1.itemindex ]._IDString
                            ),nil,MB_IconERROR);
             end;
  end else
  begin
    MessageBox(0,'Нужно удалить все пункты стандарта, прежде чем удалить стандарт',nil,MB_IconERROR);
  end;
  CloseBD([@table[ db_std ],@table[ db_chk ]]);
  table[ db_std ].ReadOnly :=true;
  ComboBox1DropDown(Combobox1);

end;

procedure TForm1.ComboBox4DropDown(Sender: TObject);
var i : integer;
begin
if (ComboBox1.itemindex = -1) then exit;
  OpenBD( [ @table[ db_chk ] ]);
  Filter(   @Table[ db_chk ], 'Check_Standard_ID = '+ c + TMP_List[ l_std ].Item[  ComboBox1.itemindex ]._IDString + c);
  TCombobox(Sender).Items.Clear;
  TMP_List[ l_chk ].Clear;

  while not table[ db_chk ].Eof do
  begin
   i := TMP_List[ l_chk ].Add( table[ db_chk ].FieldByName('Check_Name').asstring );
        TMP_List[ l_chk ].Item[i]._IDString := table[ db_chk ].FieldByName('Check_ID').asstring;
        TMP_List[ l_chk ].Item[i]._RText[0] := table[ db_chk ].FieldByName('Check_RepName').asstring;
        TCombobox(Sender).Items.Add( TMP_List[ l_chk ].Item[i]._Name );
   table[ db_chk ].Next;
  end;
  if TCombobox(Sender).Items.Count > 0 then
  begin
     TCombobox(Sender).ItemIndex := 0 ;
     Edit3.Text := TMP_List[ l_chk ].Item[0]._Name;
     Edit7.Text := TMP_List[ l_chk ].Item[0]._RText[0];
  end;
  CloseBD( [ @table[ db_chk ] ]);

end;

procedure TForm1.ComboBox4Select(Sender: TObject);
var i : integer;
begin
   if TCombobox(Sender).ItemIndex = -1 then exit;
 i := TCombobox(Sender).ItemIndex;
     Edit3.Text := TMP_List[ l_chk ].Item[i]._Name;
     Edit7.Text := TMP_List[ l_chk ].Item[i]._RText[0];
end;

procedure TForm1.Button8Click(Sender: TObject);
var id: integer;
begin
if (ComboBox4.itemindex = -1) then exit;

  table[ db_chk ].ReadOnly :=false;
  OpenBD([@table[ db_chk ]]);
  Filter(@table[ db_chk ],'Check_ID = '+C+ TMP_List[ l_chk ].Item[ ComboBox4.itemindex ]._IDString +C);

    table[ db_chk ].Edit;
      table[ db_chk ].FieldByName('Check_Name'   ).asstring   := Edit3.Text;
      table[ db_chk ].FieldByName('Check_RepName').asstring   := Edit7.Text;
    table[ db_chk ].post;

  CloseBD([@table[ db_chk ]]);
  table[ db_chk ].ReadOnly := True;
  table[ db_chk ].Filtered := False;

  id := Combobox4.ItemIndex;
  ComboBox4DropDown(Combobox4);
  Combobox4.ItemIndex := id;
  ComboBox4Select  (Combobox4);
end;

procedure TForm1.Button10Click(Sender: TObject);
begin
if  ComboBox1.itemindex = -1 then exit;
  table[ db_chk ].ReadOnly :=false;
  table[ db_chk ].Filtered :=false;
  OpenBD([@table[ db_chk ]]);
  table[ db_chk ].Append;
    table[ db_chk ].FieldByName('Check_Name'        ).asstring   := Edit3.Text;
    table[ db_chk ].FieldByName('Check_RepName'     ).asstring   := Edit7.Text;
    table[ db_chk ].FieldByName('Check_Standard_ID' ).asstring   := TMP_List[ l_chk ].Item[ ComboBox4.itemindex ]._IDString;
  table[ db_chk ].post;

  CloseBD([@table[ db_chk ]]);
  table[ db_chk ].ReadOnly := true;
  table[ db_chk ].Filtered := false;
  ComboBox4DropDown(Combobox4);

end;

procedure TForm1.Button9Click(Sender: TObject);
begin
if (ComboBox4.itemindex = -1) then exit;

  table[ db_chk ].ReadOnly :=false;
  OpenBD([ @table[ db_chk ] ]);
  Filter(  @table[ db_chk ],'Check_ID = '+C+TMP_List[ l_chk ].Item[ ComboBox4.itemindex ]._IDString+C);
     if not table[ db_chk ].eof then
            table[ db_chk ].Delete else  begin
                MessageBox(0,Pchar( 'Не найден проверочный пункт: ' +TMP_List[ l_chk ].Item[ ComboBox4.itemindex ]._Name
                           +#13+#10+'с идентификатором : '          +TMP_List[ l_chk ].Item[ ComboBox4.itemindex ]._IDString
                            ),nil,MB_IconERROR);
             end;
  CloseBD([@table[ db_chk ]]);
  table[ db_std ].ReadOnly :=true;
  ComboBox4DropDown(Combobox4);
end;

end.
