Solved

Adding Table-field?

Posted on 1997-10-14
11
177 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now