?
Solved

Rename a pagecontrol tab

Posted on 2010-01-04
2
Medium Priority
?
1,625 Views
Last Modified: 2012-05-08
Dear experts,

I have a little example that saves and loads the treeview nodes and
its text entered in the dbrichedit to and from a database.

My example works fine.

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

But now I want to make a button on my toolbar to rename the tab
And when the user does that the database-column's name that is
associated with that tab has to be renamed also.

Who can help me to solve this problem?
I have put the example in the code-section.

This what I go sofar:

procedure TMainForm.RenTabClick(Sender: TObject);
var
 NewName: String;
begin
 InputQuery('New Name','Enter something',NewName);
 PageControl1.ActivePage.Caption := NewName;
end;

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;
    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.RenTabClick(Sender: TObject);
var
 NewName: String;
begin
 InputQuery('New Name','Enter something',NewName);
 PageControl1.ActivePage.Caption := NewName;
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: 26173657
Why do you want to rename also the table field ?
Even if you change the Tabsheet Caption (that is what you're doing) you can always leave the datafield linked as it was.
Note also that changing the column name you should have problems on retrieving it during the Loadtree procedure.

BTW If you still need to rename the associated field in the Access table you should do:

Add a new column to the table with new name
Copy any value from the old column to the new column using update
Drop the old column

Note that Jet don't refresh the column count so if you add/drop column repeatedly without doing a compact and repair of the db you'll get soon an AV for reached columns limit (that is 255)
In any case, just to let you learn something new, I've changed your
procedure RenTabClick as follows, to rename your column field (but you will not able to retrieve it right after closing your app until you'll change something in the fields assignment's method in your LoadTree)
procedure TMainForm.RenTabClick(Sender: TObject);
var
  NewName, OldName: String;
  REdit: TRichEdit;
  q: TAdoQuery;
  Node: TTreeNode;
  V: Variant;
  i: integer;
begin
  (*
    Please note that this is not something that can be repeated over and over
    without a compact and repair.  JET Maintains an internal column count in a table,
    which has a limit of 255.  When you DROP the column,
    the column count is not reduced until a compact and repair is done.
    So if you add/drop over and over without a compact,
    you will quickly hit the 255 limit.
    *)
  if InputQuery('New Name', 'Enter something', NewName) then
  begin
    Node := TreeView1.Selected;
    V := integer(Node.Data);
    ADOTable1.Close;
    for i := PageControl1.ActivePage.ComponentCount - 1 downto 0 do
      if PageControl1.ActivePage.Components[i] is TDBRichEdit then
      begin
        OldName := TDBRichEdit(PageControl1.ActivePage.Components[i]).DataField;
        TDBRichEdit(PageControl1.ActivePage.Components[i]).DataField := NewName;
        break;
      end;
    PageControl1.ActivePage.Caption := NewName;
    PageControl1.ActivePage.Name := NewName;
    q := TAdoQuery.Create(nil);
    try
      q.Connection := ADOConnection1;
      q.SQL.Add('Alter table Tree Add Column '+NewName+' OLEOBJECT');
      try
        q.ExecSQL;
      except
        on E: Exception do
          showmessage('Error: '+E.Message);
      end;
      q.SQL.Clear;
      q.SQL.Add('Update Tree set '+NewName+' = '+OldName);
      try
        q.ExecSQL;
      except
        on E: Exception do
          showmessage('Error: '+E.Message);
      end;
      q.SQL.Clear;
      q.SQL.Add('Alter Table Tree Drop Column '+OldName);
      try
        q.ExecSQL;
      except
        on E: Exception do
          showmessage('Error: '+E.Message);
      end;
    finally
      q.Free;
      ADOTable1.Open;
      TreeView1.Select(Node);
      ADOTable1.Locate('ID', V, []);
    end;
    PageControl1.ActivePage.Name := NewName;
  end;
end;

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 26178299
OK.
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Integration Management Part 2
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

807 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