Solved

Insert Fields

Posted on 1998-12-08
6
222 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
loading dll from external path 1 209
Best Firemonkey component pack 1 85
Convert GUI app into console app for Win32 Env 5 78
How to debug For loops? 3 45
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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 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

14 Experts available now in Live!

Get 1:1 Help Now