Solved

Change TField.Size at runtime

Posted on 1998-10-06
6
361 Views
Last Modified: 2010-04-06
I have a table with a string field which is 6 characters in length. I want to change it to ten characters.

I need to do this at run time, so please don't suggest using Database Desktop!

Thanks.
0
Comment
Question by:philipleighs
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1341963
What Database you use?

execute a sqlstatement in a sqlquerycomponoent

the statemant maybe like this:

alter table tablename modify(fieldname newdatatype)

meikl
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1341964
Oops,

the statement above is only available on Oracle-databases

meikl
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1341965
The problem is then that you totally reconfigure your database table... Do you want that?

Paradox table?

Zif.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:ZifNab
ID: 1341966
this is an example how to change the paradox table in source :
http://www.inprise.com/devsupport/bde/files/pxrest.zip
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 50 total points
ID: 1341967
Hi philipleighs,

use for this the BDE api DBiDoRestructure :

Example 3: Alter a field in a Paradox or dBASE table.

This example will alter an existing field in a Paradox or dBASE table. NOTE: You must fill in all options in the ChangeRec with 0 or '' if the option is not used in the restructure. FillChar can be used to do this:

  Fillchar(MyChangeRec, sizeof(MyChangeRec), 0);

This example uses the following input:

  ChangeField(Table1, Table1.FieldByName('FOO'), MyChangeRec)

ChangeRec is defined as follows:

type

  ChangeRec = packed record
    szName: DBINAME;
    iType: Word;
    iSubType: Word;
    iLength: Word;
    iPrecision: Byte;
    end;


The function is defined as follows:

procedure ChangeField(Table: TTable; Field: TField; Rec: ChangeRec);

var
  Props: CURProps;
  hDb: hDBIDb;
  TableDesc: CRTblDesc;
  pFields: pFLDDesc;
  pOp: pCROpType;
  B: Byte;
begin
  // Initialize the pointers...
  pFields := nil;
  pOp := nil;
  // Make sure the table is open exclusively so we can get the db handle...
  if not Table.Active then
    raise EDatabaseError.Create('Table must be opened to restructure');
  if not Table.Exclusive then
    raise EDatabaseError.Create('Table must be opened exclusively' +

      'to restructure');
  Check(DbiSetProp(hDBIObj(Table.Handle), curxltMODE, Integer(xltNONE)));
  // Get the table properties to determine table type...
  Check(DbiGetCursorProps(Table.Handle, Props));
  // Make sure the table is either Paradox or dBASE...
  if (Props.szTableType <> szPARADOX) and (Props.szTableType <> szDBASE) then
    raise EDatabaseError.Create('Field altering can only occur on Paradox' +
      ' or dBASE tables');

  // Allocate memory for the field descriptor...
  pFields := AllocMem(Table.FieldCount * sizeof(FLDDesc));
  // Allocate memory for the operation descriptor...
  pOp := AllocMem(Table.FieldCount * sizeof(CROpType));
  try
    // Set the pointer to the index in the operation descriptor to put
    // crMODIFY (This means a modification to the record is going to happen)...
    Inc(pOp, Field.Index);
    pOp^ := crMODIFY;
    Dec(pOp, Field.Index);
    // Fill the field descriptor with the existing field information...

    Check(DbiGetFieldDescs(Table.Handle, pFields));
    // Set the pointer to the index in the field descriptor to make the
    // midifications to the field
    Inc(pFields, Field.Index);
    // If the szName portion of the ChangeRec has something in it, change it...
    if (Length(Rec.szName) > 0) then
      pFields^.szName := Rec.szName;
    // If the iType portion of the ChangeRec has something in it, change it...
    if (Rec.iType > 0) then

      pFields^.iFldType := Rec.iType;
    // If the iSubType portion of the ChangeRec has something in it, change it...
    if (Rec.iSubType > 0) then
      pFields^.iSubType := Rec.iSubType;
    // If the iLength portion of the ChangeRec has something in it, change it...
    if (Rec.iLength > 0) then
      pFields^.iUnits1 := Rec.iLength;
    // If the iPrecision portion of the ChangeRec has something
    // in it, change it...
    if (Rec.iPrecision > 0) then

      pFields^.iUnits2 := Rec.iPrecision;
    Dec(pFields, Field.Index);
    for B := 1 to Table.FieldCount do begin
      pFields^.iFldNum := B;
      Inc(pFields, 1);
    end;
    Dec(pFields, Table.FieldCount);
                         
    // Blank out the structure...
    FillChar(TableDesc, sizeof(TableDesc), #0);
    //  Get the database handle from the table's cursor handle...
    Check(DbiGetObjFromObj(hDBIObj(Table.Handle), objDATABASE, hDBIObj(hDb)));

    // Put the table name in the table descriptor...
    StrPCopy(TableDesc.szTblName, Table.TableName);
    // Put the table type in the table descriptor...
    StrPCopy(TableDesc.szTblType, Props.szTableType);
    // The following three lines are necessary when doing any field restructure
    // operations on a table...
     
    // Set the field count for the table
    TableDesc.iFldCount := Table.FieldCount;
    // Link the operation descriptor to the table descriptor...

    TableDesc.pecrFldOp := pOp;
    // Link the field descriptor to the table descriptor...
    TableDesc.pFldDesc := pFields;
    // Close the table so the restructure can complete...
    Table.Close;
    // Call DbiDoRestructure...
    Check(DbiDoRestructure(hDb, 1, @TableDesc, nil, nil, nil, False));
  finally
    if (pFields <> nil) then
      FreeMem(pFields);
    if (pOp <> nil) then

      FreeMem(pOp);
  end;
end;

Zif.
0
 
LVL 3

Author Comment

by:philipleighs
ID: 1341968
Thanks Zif,

This is exactly what I needed.

Regards,
Phil.
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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

696 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