Solved

Adding Table-field?

Posted on 1997-10-14
11
186 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:Erik N
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1347498
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
 

Expert Comment

by:Doldrums
ID: 1347499
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
 

Author Comment

by:Erik N
ID: 1347500
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 8

Expert Comment

by:ZifNab
ID: 1347501
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
 

Accepted Solution

by:
essbb earned 40 total points
ID: 1347502
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1347503
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
 

Expert Comment

by:essbb
ID: 1347504
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1347505
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1347506
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
 

Author Comment

by:Erik N
ID: 1347507
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
 

Author Comment

by:Erik N
ID: 1347508
ZifNab!
I really would like the info about DbiDoStructure (if possible)!!
Hope you see this..

Thanx!
Erik N
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

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