Adding new fields to a Paradox table from Delphi

Posted on 1999-07-05
Medium Priority
Last Modified: 2010-04-06
I want my program to add a field to the database table if
the field does not exist!
I am using Paradox 7, and Delphi4

Please give me an example!
Question by:KjellR
LVL 27

Accepted Solution

kretzschmar earned 200 total points
ID: 1386197
hi KjellR,

directly from borland


Example 10: Add a field to a Paradox or dBASE table.

   This example will add a field to the end of an existing table.
   NOTE: You must fill in all options in the ChangeRec with 0 or '' if the option is not used in the restructure.
   FillChar can be used to do this: Fillchar(MyChangeRec, sizeof(MyChangeRec), 0);
   This example uses the following input:
   AddField(Table1, MyChangeRec);
   ChangeRec is defind as follows:

   ChangeRec = packed record
       szName: DBINAME;
       iType: word;
       iSubType: word;
       iLength: word;
       iPrecision: byte;

   The function is defined as follows:

   procedure AddField(Table: TTable; NewField: ChangeRec);
     Props: CURProps;
     hDb: hDBIDb;
     TableDesc: CRTblDesc;
     pFlds: pFLDDesc;
     pOp: pCROpType;
     B: byte;

     // Make sure the table is open exclusively so we can get the db handle...
     if Table.Active = False then
       raise EDatabaseError.Create('Table must be opened to restructure');
     if Table.Exclusive = False then
       raise EDatabaseError.Create('Table must be opened exclusively to restructure');

     // Get the table properties to determine table type...
     Check(DbiSetProp(hDBIObj(Table.Handle), curxltMODE, integer(xltNONE)));
     Check(DbiGetCursorProps(Table.Handle, Props));
     pFlds := AllocMem((Table.FieldCount + 1) * sizeof(FLDDesc));
     FillChar(pFlds^, (Table.FieldCount + 1) * sizeof(FLDDesc), 0);
     Check(DbiGetFieldDescs(Table.handle, pFlds));

     for B := 1 to Table.FieldCount do begin
       pFlds^.iFldNum := B;
       Inc(pFlds, 1);
       StrCopy(pFlds^.szName, NewField.szName);
       pFlds^.iFldType := NewField.iType;
       pFlds^.iSubType := NewField.iSubType;
       pFlds^.iUnits1  := NewField.iLength;
       pFlds^.iUnits2  := NewField.iPrecision;
       pFlds^.iFldNum  := Table.FieldCount + 1;
       Dec(pFlds, Table.FieldCount);

     pOp := AllocMem((Table.FieldCount + 1) * sizeof(CROpType));
     Inc(pOp, Table.FieldCount);
     pOp^ := crADD;
     Dec(pOp, Table.FieldCount);

     // Blank out the structure...
     FillChar(TableDesc, sizeof(TableDesc), 0);
     //  Get the database handle from the table's cursor handle...
     Check(DbiGetObjFromObj(hDBIObj(Table.Handle), objDATABASE, hDBIObj(hDb)));
     // Put the table name in the table descriptor...
     StrPCopy(TableDesc.szTblName, Table.TableName);
     // Put the table type in the table descriptor...
     StrPCopy(TableDesc.szTblType, Props.szTableType);
     // Close the table so the restructure can complete...
     TableDesc.iFldCount := Table.FieldCount + 1;
     Tabledesc.pfldDesc := pFlds;
     TableDesc.pecrFldOp := pOp;
     // Call DbiDoRestructure...
       Check(DbiDoRestructure(hDb, 1, @TableDesc, nil, nil, nil, FALSE));


Author Comment

ID: 1386198
Thank you for your quick response, the points are yours :)

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
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…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…

600 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