• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

Insert Fields

How can I Insert a Field in a paradox Table in run time?
0
metodo
Asked:
metodo
  • 4
  • 2
1 Solution
 
ahalyaCommented:
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
 
metodoAuthor Commented:
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
 
ahalyaCommented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
metodoAuthor Commented:
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
 
ahalyaCommented:
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
 
ahalyaCommented:
did you try the above ?
0
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now