[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Creating a new db-column by adding a new tab to a PageControl.

Posted on 2010-01-02
7
Medium Priority
?
330 Views
Last Modified: 2012-05-08
Dear experts,

I have a little example that contains a Treeview on the left, a PageControl on the right
with on tab (tabsheet1) and on the tab I have a DBRichEdit1 set to Align = AlClient.
And 2 buttons for making a file and a folder node in the Treeview. And 3 non-visual
components: ADOConnection1, ADOTable1 and a DataSource1.

This little example loads and saves the treeview-nodes along with its associated text
that will be displayed on the dbrichedit to and from a database. Everthing works great.

So, everytime a user creates a file or a folder node in the treeview, a new record will be added
and if the user puts text in to the dbrichedit it will be saved also to column Data1 of that record.
But now I want to give the user the possibility to add new columns to the database by creating
new tabs (tabsheets) on the pagecontrol with a third button (called NewTab1).

I have allready tried to write it myself, and delete the tab with the dbrichedit on it.
And I have written some code to create the first tab dynamicly at startup. And it works
fine for the first tab, the problem is when there a more tabs added:

procedure TForm1.NewTab1Click(Sender: TObject);
begin
  NewTab;
end;

procedure TForm1.FormCreate(Sender: TObject);
Var
 i:integer;
 n,n2:TTreeNode;
begin
 Table.First;
 With TreeView do
  begin
   Items.BeginUpdate;
   Items.Clear;
   while Not Table.Eof do
    begin
     n2:=FindNode(TableParent.Value);
     n:=Items.AddChild(n2,TableName.Value);
     n.ImageIndex:=TableImage_Index.Value;
     n.SelectedIndex:=n.ImageIndex;
     n.Data:=Pointer(TableID.Value);
     Table.Next;
    end;
   for i:=0 to Items.Count-1 do Items[i].Expand(True);
   Items.EndUpdate;
  end;
    NewTab;
end;

procedure TForm1.NewTab;
var
   aForm : TDBRichEdit;
   tabSheet : TTabSheet;
begin
   //Make a new tab
   tabSheet := TTabSheet.Create(PageControl1) ;
   tabSheet.PageControl := PageControl1;

   //Create the dbrichedit
   aForm := TDBRichEdit.Create(tabSheet) ;
   aForm.Parent := tabSheet;
   aForm.Name := 'DBRichedit1';                   <=====enumerate, how?
   aForm.Align := alClient;
   aForm.Visible := true;
   aForm.DataSource := DataSource1;  
   aForm.DataField := 'Data1';                           <=====enumerate, how?
   tabsheet.Name := 'TabSheet1';                                  <=====enumerate, how?
   tabsheet.caption := 'New Tab' + '(' + IntToStr(tabsheet.tabindex+1) + ')';  <=====enumerate, this works.

   //Activate the tab
   PageControl1.ActivePage := tabSheet;
end;

Who knows the solution and is willing to help me.
I have put the whole example in the code-section.

Greetings,

Peter Kiers
unit Main;

interface

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

type
  TForm1 = class(TForm)
    TreeView: TTreeView;
    PageControl1: TPageControl;
    Panel1: TPanel;
    DBText1: TDBText;
    DBText2: TDBText;
    btnAddMain: TButton;
    DBEdtName: TDBEdit;
    btnAddChild: TButton;
    ADOConnection1: TADOConnection;
    ADOTable1: TADOTable;
    ADOTable1ID: TAutoIncField;
    ADOTable1Parent: TIntegerField;
    ADOTable1Status: TWordField;
    ADOTable1Name: TWideStringField;
    ADOTable1Image_Index: TSmallintField;
    ADOTable1Data1: TBlobField;
    ADOTable1Data2: TBlobField;
    DataSource1: TDataSource;
    NewTab1: TButton;
    procedure NewTab1Click(Sender: TObject);
    procedure btnAddMainClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure DBEdtNameChange(Sender: TObject);
    procedure TreeViewChange(Sender: TObject; Node: TTreeNode);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    Procedure NewTab;
    function FindNode(ni:integer):TTreeNode;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.btnAddMainClick(Sender: TObject);
Var
 n,n2:TTreeNode;
begin
 if (Sender=btnAddChild) And Assigned(TreeView.Selected) Then n2:=TreeView.Selected Else n2:=nil;
 n:=TreeView.Items.AddChild(n2,'New');
 TreeView.Selected:=nil;
 ADOTable1.Insert;
 ADOTable1Status.Value:=0;
 ADOTable1Name.Value:=n.Text;
 ADOTable1Image_Index.Value:=0;
 if Assigned(n2)
  Then ADOTable1Parent.Value:=Integer(n2.Data)
  Else ADOTable1Parent.Value:=0;
 ADOTable1.Post;
 n.Data:=Pointer(ADOTable1ID.Value);
 TreeView.Selected:=n;
end;
(*---------------------------------------------------------*)
procedure TForm1.NewTab1Click(Sender: TObject);
begin
  NewTab;
end;
(*---------------------------------------------------------*)
procedure TForm1.DBEdtNameChange(Sender: TObject);
begin
 if Assigned(TreeView.Selected) then TreeView.Selected.Text:=DBEdtName.Text;
end;
(*---------------------------------------------------------*)
function TForm1.FindNode(ni: integer): TTreeNode;
Var
 i:integer;
begin
 With TreeView 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 TForm1.FormCreate(Sender: TObject);
Var
 i:integer;
 n,n2:TTreeNode;
begin
 ADOTable1.First;
 With TreeView do
  begin
   Items.BeginUpdate;
   Items.Clear;
   while Not ADOTable1.Eof do
    begin
     n2:=FindNode(ADOTable1Parent.Value);
     n:=Items.AddChild(n2,ADOTable1Name.Value);
     n.ImageIndex:=ADOTable1Image_Index.Value;
     n.SelectedIndex:=n.ImageIndex;
     n.Data:=Pointer(ADOTable1ID.Value);
     ADOTable1.Next;
    end;
   for i:=0 to Items.Count-1 do Items[i].Expand(True);
   Items.EndUpdate;
  end;
    NewTab;
end;
(*---------------------------------------------------------*)
procedure TForm1.FormDestroy(Sender: TObject);
begin
 ADOTable1.First;
end;
(*---------------------------------------------------------*)
procedure TForm1.NewTab;
var
   aForm : TDBRichEdit;
   tabSheet : TTabSheet;
begin
   //Make a new tab
   tabSheet := TTabSheet.Create(PageControl1) ;
   tabSheet.PageControl := PageControl1;

   //Create the dbrichedit
   aForm := TDBRichEdit.Create(tabSheet) ;
   aForm.Parent := tabSheet;
   aForm.Name := 'DBRichedit1';                   
   aForm.Align := alClient;
   aForm.Visible := true;
   aForm.DataSource := DataSource1;  
   aForm.DataField := 'Data1';                           
   tabsheet.Name := 'TabSheet1';                                  
   tabsheet.caption := 'New Tab' + '(' + IntToStr(tabsheet.tabindex+1) + ')';   

   //Activate the tab
   PageControl1.ActivePage := tabSheet;
end;
(*---------------------------------------------------------*)
procedure TForm1.TreeViewChange(Sender: TObject; Node: TTreeNode);
Var
 V:Variant;
begin
 if Assigned(TreeView.Selected) then
  begin
   V:=Integer(TreeView.Selected.Data);
   if V>0 then ADOTable1.Locate('ID',V,[]);
  end;
end;
(*---------------------------------------------------------*)
end.

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 412
  ClientWidth = 578
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  WindowState = wsMaximized
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object TreeView: TTreeView
    Left = 0
    Top = 36
    Width = 209
    Height = 376
    Align = alLeft
    HideSelection = False
    Indent = 19
    RowSelect = True
    TabOrder = 0
    OnChange = TreeViewChange
  end
  object PageControl1: TPageControl
    AlignWithMargins = True
    Left = 209
    Top = 39
    Width = 369
    Height = 373
    Margins.Left = 0
    Margins.Right = 0
    Margins.Bottom = 0
    Align = alClient
    TabOrder = 1
  end
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 578
    Height = 36
    Align = alTop
    TabOrder = 2
    object DBText1: TDBText
      Left = 136
      Top = 12
      Width = 24
      Height = 17
      Hint = 'ID'
      DataField = 'ID'
      DataSource = DataSource1
      ParentShowHint = False
      ShowHint = True
    end
    object DBText2: TDBText
      Left = 344
      Top = 12
      Width = 24
      Height = 17
      Hint = 'Parent'
      DataField = 'Parent'
      DataSource = DataSource1
      ParentShowHint = False
      ShowHint = True
    end
    object btnAddMain: TButton
      Left = 4
      Top = 4
      Width = 64
      Height = 28
      Caption = 'Add Main'
      TabOrder = 0
      OnClick = btnAddMainClick
    end
    object DBEdtName: TDBEdit
      Left = 162
      Top = 9
      Width = 176
      Height = 21
      DataField = 'Name'
      DataSource = DataSource1
      TabOrder = 1
      OnChange = DBEdtNameChange
    end
    object btnAddChild: TButton
      Left = 68
      Top = 4
      Width = 64
      Height = 28
      Caption = 'Add Child'
      TabOrder = 2
      OnClick = btnAddMainClick
    end
    object NewTab1: TButton
      Left = 374
      Top = 8
      Width = 51
      Height = 25
      Caption = 'New Tab'
      TabOrder = 3
      OnClick = NewTab1Click
    end
  end
  object ADOConnection1: TADOConnection
    Connected = True
    ConnectionString = 
      'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Peter\Desk' +
      'top\PageControl\bdtree.mdb;Persist Security Info=False'
    LoginPrompt = False
    Mode = cmShareDenyNone
    Provider = 'Microsoft.Jet.OLEDB.4.0'
    Left = 48
    Top = 48
  end
  object ADOTable1: TADOTable
    Active = True
    Connection = ADOConnection1
    CursorType = ctStatic
    TableName = 'Tree'
    Left = 80
    Top = 48
    object ADOTable1ID: TAutoIncField
      FieldName = 'ID'
      ReadOnly = True
    end
    object ADOTable1Parent: TIntegerField
      FieldName = 'Parent'
    end
    object ADOTable1Status: TWordField
      FieldName = 'Status'
    end
    object ADOTable1Name: TWideStringField
      FieldName = 'Name'
      Size = 50
    end
    object ADOTable1Image_Index: TSmallintField
      FieldName = 'Image_Index'
    end
    object ADOTable1Data1: TBlobField
      FieldName = 'Data1'
    end
    object ADOTable1Data2: TBlobField
      FieldName = 'Data2'
    end
  end
  object DataSource1: TDataSource
    DataSet = ADOTable1
    Left = 120
    Top = 48
  end
end

Open in new window

0
Comment
Question by:peterkiers
  • 4
  • 3
7 Comments
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26165216
What do you mean? You want to assign an existent datax to the dbrichedit of the new tab or need to add also the new field to the access table?
In the first case, if you already have let's say 10 fields named from data1 to data10 you can simply use the same method already used for the caption.

 aForm := TDBRichEdit.Create(tabSheet) ;
   aForm.Parent := tabSheet;
   aForm.Name := 'DBRichedit'+ IntToStr(tabsheet.tabindex+1;                   <=====enumerate, how?
   aForm.Align := alClient;
   aForm.Visible := true;
   aForm.DataSource := DataSource1;  
   aForm.DataField := 'Data'+ IntToStr(tabsheet.tabindex+1;                           <=====enumerate, how?
   tabsheet.Name := 'TabSheet'+ IntToStr(tabsheet.tabindex+1;                                  <=====enumerate, how?
   tabsheet.caption := 'New Tab' + '(' + IntToStr(tabsheet.tabindex+1) + ')';  <=====enumerate, this works.

but if you want t add the column you need to alter ta table before and add the new field to the table.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 26168479
"need to add also the new field to the access table?"
Is that possible?

Greetings,
Peter Kiers
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26169698
Yes, it's possible, but there are some substantial changes to do in your program.
First of all we have to delete any field component, because to know if a field already exists we must search for it using findfield, and if we have persistent fields we cannot find any other field added on runtime as it will return always nil.

Then we must add the new column (field) to the table altering it using sql.

So, in little words:

On start we'll create a tab for any field Datax found.
Adding new Tab we'll search for the corresponding DataX field and if it not exists we'll create it.

See the new code attached (no object added to the form, so just copy and past the unit code).
unit Main;

interface

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

type
  TForm1 = class(TForm)
    TreeView: TTreeView;
    PageControl1: TPageControl;
    Panel1: TPanel;
    DBText1: TDBText;
    DBText2: TDBText;
    btnAddMain: TButton;
    DBEdtName: TDBEdit;
    btnAddChild: TButton;
    ADOConnection1: TADOConnection;
    ADOTable1: TADOTable;
    DataSource1: TDataSource;
    NewTab1: TButton;
    procedure NewTab1Click(Sender: TObject);
    procedure btnAddMainClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure DBEdtNameChange(Sender: TObject);
    procedure TreeViewChange(Sender: TObject; Node: TTreeNode);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    Procedure NewTab;
    function FindNode(ni: integer): TTreeNode;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnAddMainClick(Sender: TObject);
Var
  n, n2: TTreeNode;
  p: integer;
begin
  if (Sender=btnAddChild) And Assigned(TreeView.Selected) Then
    n2 := TreeView.Selected
  Else
    n2 := nil;
  n := TreeView.Items.AddChild(n2, 'New');
  TreeView.Selected := nil;
  ADOTable1.Insert;
  ADOTable1.FieldByName('Status').Value := 0;
  ADOTable1.FieldByName('Name').Value := n.Text;
  ADOTable1.FieldByName('Image_Index').Value := 0;
  if Assigned(n2) Then
    ADOTable1.FieldByName('Parent').Value := integer(n2.Data)
  Else
    ADOTable1.FieldByName('Parent').Value := 0;
  ADOTable1.Post;
  p := ADOTable1.FieldByName('ID').Value;
  n.Data := Pointer(p);
  TreeView.Selected := n;
end;

(* --------------------------------------------------------- *)
procedure TForm1.NewTab1Click(Sender: TObject);
begin
  NewTab;
end;

(* --------------------------------------------------------- *)
procedure TForm1.DBEdtNameChange(Sender: TObject);
begin
  if Assigned(TreeView.Selected) then
    TreeView.Selected.Text := DBEdtName.Text;
end;

(* --------------------------------------------------------- *)
function TForm1.FindNode(ni: integer): TTreeNode;
Var
  i: integer;
begin
  With TreeView 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 TForm1.FormCreate(Sender: TObject);
Var
  i, p: integer;
  n, n2: TTreeNode;
begin
  ADOTable1.First;
  With TreeView do
  begin
    Items.BeginUpdate;
    Items.Clear;
    while Not ADOTable1.Eof do
    begin
      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 TForm1.FormDestroy(Sender: TObject);
begin
  ADOTable1.First;
end;

(* --------------------------------------------------------- *)
procedure TForm1.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 := TreeView.Selected;
    V := integer(Node.Data);
    ADOTable1.Close;
    CreateColumn('Data' + IntToStr(tabSheet.tabindex+1));
    ADOTable1.Open;
    TreeView.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 TForm1.TreeViewChange(Sender: TObject; Node: TTreeNode);
Var
  V: Variant;
begin
  if Assigned(TreeView.Selected) then
  begin
    V := integer(TreeView.Selected.Data);
    if V>0 then
      ADOTable1.Locate('ID', V, []);
  end;
end;

(* --------------------------------------------------------- *)
end.

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:peterkiers
ID: 26170039
Hi, sorry for a late response, will not happen again.

I have alter the code like you said but I get an error-message.

"The field Data2 allready exist in table"

Could you check if the code that I have put in the code-section is allright.

Greetings, Peter Kiers

0
 
LVL 1

Author Comment

by:peterkiers
ID: 26170053
Forgot the code...
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;
    Button1: TButton;
    Button2: TButton;
    ADOTable1ID: TAutoIncField;
    ADOTable1Parent: TIntegerField;
    ADOTable1Status: TWordField;
    ADOTable1Name: TWideStringField;
    ADOTable1Image_Index: TSmallintField;
    ADOTable1Data1: TBlobField;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(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.Button1Click(Sender: TObject);
begin
newtab;
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
      n2 := FindNode(ADOTable1Parent.Value);
      n := Items.AddChild(n2, ADOTable1Name.Value);
      n.ImageIndex := ADOTable1Image_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;
  if (ADOTable1.FindField('Data' + IntToStr(tabSheet.tabindex+1)) = nil) then
  begin
    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
 
LVL 23

Accepted Solution

by:
Ferruccio Accalai earned 2000 total points
ID: 26170192
You must delete persistent fields from adotable (no fields added, fields editor must be empty)
So remove
    ADOTable1ID: TAutoIncField;
    ADOTable1Parent: TIntegerField;
    ADOTable1Status: TWordField;
    ADOTable1Name: TWideStringField;
    ADOTable1Image_Index: TSmallintField;
    ADOTable1Data1: TBlobField;

Then modify the LoadTree Function as follows
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;

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 26170407
PERFECT. 500 points are comming to you...

Greetings,

Peter Kiers
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

831 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