Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Adding Fields to Existing table

Posted on 1998-10-01
4
Medium Priority
?
481 Views
Last Modified: 2010-04-06
Is it possible to add new fields to an existing table at runtime. I have looked at help files regarding fielddefs.add, but it seems to indicate this is for use only on new table creation.

I have an existing database application, but I want to be able to add new functionality requiring new fields, but I don't want to erase existing data.
0
Comment
Question by:JohnE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
BoRiS earned 200 total points
ID: 1341396
JohnE

her is one way to do just that, this addes a stringfiled to the dataset...

procedure TForm1.AddFieldButClick(Sender: TObject);

var

  NewField: TStringField;

begin

  Table1.Close;

  NewField := TStringField.Create(Self);

  NewField.FieldName := 'NEW_FIELD';

  NewField.Name := Table1.Name + NewField.FieldName;

  NewField.Index := Table1.FieldCount;

  NewField.DataSet := Table1;

  Table1.FieldDefs.UpDate;

  Table1.Open;

end;

Later
BoRiS
0
 

Expert Comment

by:dejohn99
ID: 1341397
BoRiS' answer is incomplete - it will add a new field to the internal representation of the dataset, however it will not actually add it to the tables!

The complete algorithm should be

1. Rename the existing table to something innocuous like "WORKING"

2. Create a new TTable object with the appropriate paths, names, etc.

3. Copy the field structure of the working table structure to the field structure of the new TTable object

4. Call TTable.Create  and TTable.Open with the new TTable object

5. Either use BatchMove or a "While not WorkingTable.EOF" loop to read records from the working table and insert them into the new table.  Ensure that your field transfer method uses fields by name

6. Close both the new table and the working table

7. Make modifications to the old TTable object as noted by BoRiS

8. Re-open the original Table object (which now sees the new field)

Procedure btnAddFieldClick (Sender);
{
    Table1 - the table we are changing
    WorkingTable - the internal representation of the table we are using to work with
    NewTable - The output of the final (revised) table structure
}
var
    WorkingTable, NewTable: TTable;
    i: Integer;
begin
    Screen.Cursor := crHourGlass;
    Table1.Close;
    WorkingTable := TTable.Create (Self);
    NewTable := TTable.Create (Self);
    try
        with WorkingTable do begin
            DatabaseName := Table1.DatabaseName;
            TableName := Table1.TableName;
            Open;
            RenameTable ('WORKING');
        end;

        with NewTable do begin
            DatabaseName := Table1.DatabaseName;
            TableName := Table1.TableName;
            for i := 0 to  WorkingTable.FieldCount-1 do begin
                TFieldDef.Create (FieldDefs,
          WorkingTablel.Fields[i].FieldName,
          WorkingTable.Fields[i].DataType,
          WorkingTable.Fields[i].Size,
          WorkingTable.Fields[i].Required,
                    i);
            end; {for}
            TFieldDef.Create (FieldDefs,
                'NewField',
                dtString,
                100,
                false,
                FieldCount);

            CreateTable;
            WorkingTable.First;
            while not WorkingTable.EOF do begin
                Insert;
                for i := 0 to  WorkingTable.FieldCount-1 do begin
                    FieldByName (WorkingTable.Fields[i]).FieldName) .Value := WorkingTable.Fields[i].Value;
                end;
                Post;
                WorkingTable.Next;
            end;
            Close;
        end; {with}
        WorkingTable.Close;

    finally
        WorkingTable.Destroy;
        NewTable.Destroy;
        Table1.Open;
        Screen.Cursor := crArrow;
    end;
end;
0
 

Expert Comment

by:dejohn99
ID: 1341398
oops!  I forgot to add the new field to the existing TTable object.

Use BoRiS's code for that final step.
0
 

Author Comment

by:JohnE
ID: 1341399
Thanks, we dejohn99's comments, it turned out great!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

604 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