Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Insert Fields

Posted on 1998-12-08
6
Medium Priority
?
229 Views
Last Modified: 2010-04-04
How can I Insert a Field in a paradox Table in run time?
0
Comment
Question by:metodo
[X]
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
  • 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
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.

 

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 400 total points
ID: 1349894
did you try the above ?
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

649 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