Solved

Insert Fields

Posted on 1998-12-08
6
221 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
find a node in VST 2 47
LAN or WAN ? 11 66
Delphi inherited method 6 42
Base1 Encode/Decode 3 39
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

12 Experts available now in Live!

Get 1:1 Help Now