?
Solved

Deleting a db-column by deleting a tab of a PageControl.

Posted on 2010-01-04
2
Medium Priority
?
322 Views
Last Modified: 2012-05-08
Dear Experts,

I have a little example that loads and saves the treeview-nodes and the
text (that will be displayed in a dbrichedit) to/from a database.

In my previous question I have asked how to add a new column of a database
by adding a new tab of a pagecontrol. I have received a very good solution.

Now I want to make another button to delete a db-column by deleting a tab
of a pagecontrol. Who can help me with this.

I have put the code of the example in the code-section.

Greetings, Peter Kiers



unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, DB, ADODB, DBCtrls, StdCtrls, Menus;

type
  TMainForm = class(TForm)
    TreeView1: TTreeView;
    PageControl1: TPageControl;
    Panel1: TPanel;
    ADOConnection1: TADOConnection;
    ADOTable1: TADOTable;
    DataSource1: TDataSource;
    AddTab: TButton;
    RenTab: TButton;
    DelTab: TButton;
    procedure DelTabClick(Sender: TObject);
    procedure RenTabClick(Sender: TObject);
    procedure AddTabClick(Sender: TObject);
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    DBPath: string; 
    DBName: string; 
    procedure OpenDatabase;  
    function FindNode(ni: integer): TTreeNode;
    Procedure NewTab;
  public
    { Public declarations }
    procedure LoadTree;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

procedure TMainForm.AddTabClick(Sender: TObject);
begin
  newtab;
end;



procedure TMainForm.DelTabClick(Sender: TObject);
begin
 //??????????
end;

function TMainForm.FindNode(ni: integer): TTreeNode;
var
  i: integer;
begin
  with treeview1 do
   for i := 0 to Items.Count - 1 do
    if ni = Integer(Items[i].Data) then
      begin
        Result := Items[i];
        Exit;
      end;
  Result := nil;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  DBPath := IncludeTrailingPathDelimiter(ExtractFilePath(Application.Exename));
  DBName := 'dbtree.mdb';
  OpenDatabase; 
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  AdoTable1.First;
end;

procedure TMainForm.LoadTree;
var
  i, p: integer;
  n, n2: TTreeNode;
begin
  ADOTable1.First;
  with TreeView1 do
  begin
    Items.BeginUpdate;
    Items.Clear;
    while not ADOTable1.Eof do
    begin
    //we use fieldbyname because we have deleted the persistent fields
     n2 := FindNode(ADOTable1.FieldByName('Parent').Value);
      n := Items.AddChild(n2, ADOTable1.FieldByName('Name').Value);
      n.ImageIndex := ADOTable1.FieldByName('Image_Index').Value;
      n.SelectedIndex := n.ImageIndex;
      p := ADOTable1.FieldByName('ID').Value;
      n.Data := Pointer(p);
      ADOTable1.Next;
    end;
    for i := 0 to Items.Count - 1 do Items[i].Expand(True);
    Items.EndUpdate;
    end;
 //let's create any tab for any field Datax found;
  for i := 1 to Adotable1.FieldCount do
    if (Adotable1.FindField('Data'+inttostr(i)) <> nil) then
       NewTab;
    PageControl1.TabIndex := 0;
end;


procedure TMainForm.NewTab;
  procedure CreateColumn(ColumnName: String);
  var
    q: TAdoQuery;
  begin
    q := TAdoQuery.Create(nil);
    try
      q.Connection := ADOConnection1;
      q.SQL.Add('Alter table Tree Add Column '+ColumnName+' OLEOBJECT');
      try
        q.ExecSQL;
      except
        on E: Exception do
          showmessage('Error: '+E.Message);
      end;
    finally
      q.Free;
    end;
  end;
var
  aForm: TDBRichEdit;
  tabSheet: TTabSheet;
  Node: TTreeNode;
  V: Variant;
begin
  // Make a new tab
  tabSheet := TTabSheet.Create(PageControl1);
  tabSheet.PageControl := PageControl1;
  // Create the dbrichedit
  aForm := TDBRichEdit.Create(tabSheet);
  aForm.Parent := tabSheet;
  aForm.Name := 'DBRichedit' + IntToStr(tabSheet.tabindex+1);
  aForm.Align := alClient;
  aForm.Visible := True;
  aForm.DataSource := DataSource1;
  //Does the datafield already exists?
  if (ADOTable1.FindField('Data' + IntToStr(tabSheet.tabindex+1)) = nil) then
  begin
    //if not exists we have to create it, altering the table by sql
    //this can do only if the table is closed
    //so we must bookmark the record to get it back after the column addiction

    Node := TreeView1.Selected;
    V := integer(Node.Data);
    ADOTable1.Close;
    CreateColumn('Data' + IntToStr(tabSheet.tabindex+1));
    ADOTable1.Open;
    TreeView1.Select(Node);
    ADOTable1.Locate('ID', V, []);
  end;
  aForm.DataField := 'Data' + IntToStr(tabSheet.tabindex+1);
  tabSheet.Name := 'TabSheet' + IntToStr(tabSheet.tabindex+1);
  tabSheet.caption := 'New Tab' + '(' + IntToStr(tabSheet.tabindex+1) + ')';
  // Activate the tab
  PageControl1.ActivePage := tabSheet;
end;

procedure TMainForm.OpenDatabase;
var
  conn_str: string;
begin
  conn_str :=
     Format('provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=%s%s;',
    [IncludeTrailingPathDelimiter(DBPath), DBName]);
  ADOConnection1.Connected := False;   
  ADOConnection1.ConnectionString := conn_str;
  ADOConnection1.Connected := True;    
  ADOTable1.Open;   
  LoadTree;     
end;

procedure TMainForm.TreeView1Change(Sender: TObject; Node: TTreeNode);
Var
 V:Variant;
begin
 if Assigned(TreeView1.Selected) then
  begin
   V:=Integer(TreeView1.Selected.Data);
   if V>0 then ADOTable1.Locate('ID',V,[]);
  end;
end;

end.

Open in new window

0
Comment
Question by:peterkiers
2 Comments
 
LVL 23

Accepted Solution

by:
Ferruccio Accalai earned 2000 total points
ID: 26170760
In my opinion you should not remove the column from the DB as you could have any other node with text in that field column.

So your solution should be to leave the column fields and just show or hide the tab if value in DataX is present.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 26170856
OK thank for your advice.

Peter
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question