Solved

Insert Fields

Posted on 1998-12-08
6
225 Views
Last Modified: 2010-04-04
How can I Insert a Field in a paradox Table in run time?
0
Comment
Question by:metodo
  • 4
  • 2
6 Comments
 
LVL 7

Expert Comment

by:ahalya
ID: 1349889
This is "very" tricky. you do it through BDE calls.   Here is an example i picked up somewhere in Borland's web site a while back.

This procedure adds a string type field to an existing table.  I call it from my code as
  AppendANewStringField('NewFieldName');

Remember,
   1.  you need Exclusive access to the table to do this.
   2. add "BDE" to your uses clause.

btw, i have defined TCurrencyString as string[03];

=========================================================
procedure TForm1.AppendANewStringField(NewFieldName:TCurrencyString);


type
  FLDDescs = array [1..200] of FLDDesc;   // these are kludges for ease of addressing
  PFLDDescs = ^FLDDescs;
  CROpTypes = array [1..200] of CROpType;
  PCROpTypes = ^CROpTypes;

var
  props:     CURProps;    // table properties
  descs:     pFLDDescs;   // field descriptors
  ops:       pCROpTypes;  // field operators
  tabledesc: CRTblDesc;   // table descriptor for restructure
  i:         integer;

begin
Screen.Cursor := crHourglass;
with Table1 do
   try
   if not active then Open;
   Check(DbiGetCursorProps(Handle,props));
   // make arrays big enough to hold existing fields, plus 2 new fields
   descs := AllocMem(sizeof(FLDDesc) * (props.iFields + 2));
   ops := AllocMem(sizeof(CROpType) * (props.iFields + 2));

   try
      Check(DbiGetFieldDescs(Handle,@descs[1]));
      for i := 1 to props.iFields do   // retain existing fields
          ops^[i] := crNOOP;
      i := props.iFields + 1;

      //------ add first new field --------------
      ops^[i] := crADD;
      with descs^[i] do
           begin
           //  iFldNum := i;  (leave 0 when performing crADD)
           StrCopy(szName, pchar(NewFieldName)); //'Initials');
           iFldType := fldFLOAT;
           iUnits1 := 5;   // number of characters
           end;

        // Finally, feed the new field desciption into a new table descriptor.
        FillChar(tabledesc,sizeof(CRTblDesc),#0);
        with tabledesc do
          begin
            StrPCopy(szTblName,TableName);  // ie. ChildTable.TableName
          // StrCopy(szTblType,szParadox);
          // bPack := True;
            iFldCount := props.iFields+1;
            pecrFldOp := @ops^[1];
            pfldDesc := @descs^[1];
          end;
        Close;  // table must be closed as restructure requires exclusive access
        try
        Check(DbiDoRestructure(Database1.Handle,
                               1,
                               @tabledesc,
                               nil,
                               nil,
                               nil,
                               False));
    except
      on E:EDatabaseError do
         begin;
         if pos('table is busy', lowercase(E.Message)) <> 0 then
            MessageDlg('Error creating New Data Field'#13'Program Needs exclusive access to the Database', mtError, [mbOk], 0)
         else MessageDlg('DB Error: ' + E.Message, mtError, [mbOk], 0);
         end;
      on E:Exception do
            ShowMessage('Unknown Exception Adding New Field : ' + E.Message);
      end;
      finally
        FreeMem(descs);
        FreeMem(ops);
      end;
    finally
      Screen.Cursor := crDefault;
    end;
end;



0
 

Author Comment

by:metodo
ID: 1349890
This procedure operates correctly, but only add a field at final of the structure.

The one which I need is to insert a field in certain position of the structure of the table.

Do you have any suggestion?
0
 
LVL 7

Expert Comment

by:ahalya
ID: 1349891
Metodo !

you should have asked for a suggestion before rejecting the comment, IMHO. The code appends a field at the end, because that's what it's designed to do !!

if you had studied the code, you could definitely have found out how to insert a field,  (code is commented enough, to let one figure this out).

If you want to insert a field, obviously, YOU rearrange the fields in the "DESCS" variable (in the above code, the new fields are added at the end- see comments within the code).

once again,  i strongly feel that you were very UNFAIR in rejecting that answer.  (Not that it matters to me in one way or the other !)

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:metodo
ID: 1349892
ahalya

Sorry for rejecting, I didn't know that make, so then select this option.

And, yes, you are right, and I already made it, but the one which makes this example is that insert the field in the correc place, but the information are deleted and I not understand why

I would thank that you could help me in thisme
0
 
LVL 7

Expert Comment

by:ahalya
ID: 1349893
i don't exactly understand your problem:

hope you have noted the lines :

       ops^[i] := crNOOP;   //used to retain the existing fields

and
        ops^[i] := crADD; //used to add the fields
in my code.

i'd try help on crAdd, cdNoOp, cdDelete etc. Open up your BDE32.HLP in the "../Common Files/BDE" directory and look up.

0
 
LVL 7

Accepted Solution

by:
ahalya earned 100 total points
ID: 1349894
did you try the above ?
0

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.

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

Suggested Solutions

Title # Comments Views Activity
Downloading email attachments 2 79
creating threads in delphi 1 130
When i run adoquery my application freezes 26 178
Graphics32 under Delphi 10.1 Berlin 2 98
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 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