Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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
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
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

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

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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

671 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