Adding Table-field?

Is it possible to add a field to a table ?
I tried to add field 'test'  like this:
Table1.Active:=False;
With Table1.FieldDefs do
Begin
Add('test',ftInteger,0,False);
End;
CreateTable;
Table1.Active:=True;

This works, but the information already in the table disappears (of course). Can I add a field without emptying the table?

Ps. I use Delphi Developer 2.0 and the table is a DBase IV.

Thank you
Erik N
Erik NAsked:
Who is Participating?
 
essbbConnect With a Mentor Commented:
You need to use the batchmove component.

Follow these steps

1 create a temporary table with the new field
2 Use Batchmove to copy the existing records into the temp table
3 delete original table
4 rename temporary table to original table.

This could be dangerous in a multi-user environment.
If you need multiuser support use the SQL
0
 
ZifNabCommented:
It's normal that you get an empty table. CreateTable is your problem. I'll look for a solution:

The CreateTable method creates a new empty database table. Before calling this method, the DatabaseName, TableName, TableType, FieldDefs and IndexDefs properties must be assigned values.
0
 
DoldrumsCommented:
The easiest way to do this would be to use an SQL statement.  Here is a code snippet for you: (must use DB, DBTables)============================
  with TQuery.Create(Application) do
    try
      SQL.Add('ALTER TABLE "<your tables path full path.dbf>"
              ADD <your field name> <data type');
      ExecSQL;
    finally
      free;
    end;
========================
See CREATE TABLE in your Borland\Common Files\BDE\LocalSQL.hlp for the allowed data_type's.

An example for the add statement would be:
  SQL.Add('ALTER TABLE "C:\test.dbf" ADD NAME CHAR(15)');


You could probably also do it with a DBI call... but it would be much more difficult.
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
Erik NAuthor Commented:
Sorry Doldrum, SQL-statements is like chinese for me. I would like to do this without using SQL, if possible....

To ZifNab: I appreciate it !!

Thanx !!
Erik N.
0
 
ZifNabCommented:
Sorry, at the moment I can't find an easy solution. But may I ask why you need a field to a table? Why don't you just use a calculated field?
At the moment : If you don't want to use SQL, you can create a totally new table with your new field and then use the TBatchmove component in mode batUpdate. To copy the records for your first table to your new table.
But still, I'd like to have an answer to my quistion, if it's possible.
0
 
ZifNabCommented:
essbb,
I don't know if you know it, but your answer looks very much like my comment. I hope you didn't saw this, because otherwise I get strange feelings about it.

0
 
essbbCommented:
No I didn't see your comment last time. If you ment is as an answer you should have indicated this and the system would have locked me out.
0
 
ZifNabCommented:
essbb, didn't gave it as an answer because it's not really a beautifull answer. But if he likes it, he could use it.
Have fun,
ZifNab;
0
 
ZifNabCommented:
Erik N!
Sorry, I made a mistake! It's possible, I'll give you the function: DbiDoRestructure.
Want more information, just ask!
c.u. ZifNab;

0
 
Erik NAuthor Commented:
Thank you essbb for your answer!

I would also like to thank ZifNab for his comments..
(I want the user to be able to add a field to the table in order to adapt the application to new situations. So the application won't be crap the minute the situation changes.)


I guess the only right thing to do here is to use SQL after all...

Erik N
0
 
Erik NAuthor Commented:
ZifNab!
I really would like the info about DbiDoStructure (if possible)!!
Hope you see this..

Thanx!
Erik N
0
All Courses

From novice to tech pro — start learning today.