Solved

crMODIFY & DBase Files. How to use?????:(

Posted on 1998-07-21
7
307 Views
Last Modified: 2013-11-24
My question is as follows,

How on earth do you use crMODIFY to alter the fields in a dataset other than the first one. I can use it successfully to edit the first fields Type,Width and precision but am unable to modify any other field as I keep getting
"invalid restructure operation" . Does anyone have some code to show the correct way to step through a list of field descriptors as I am at my wits end trying to solve this. Surely someone has done this before?? I can also use the crAdd and crDrop successfully but the crMODIFY has got me stumped. I have already had a look at some source that just shows how to Add a new field but gee I need something more than that as I has adjusted the code to suit a modify operation and as I said it does alter/modify the first field descs in the DBase table but when it tries to do the DbiDoRestructure on the second field it crashes out every time. Anyone ??

Westy (PS Good points for this)

0
Comment
Question by:westy100697
  • 4
  • 3
7 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 250 total points
ID: 1358282
Hi westy,

have you already looked at the great examples at Inprise concerning DBE?

Guess this is exactly what you need, isn't it?

Here is an example :

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 defind as follows:

                         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 Table.Active = False then
                             raise EDatabaseError.Create('Table must be opened to restructure');
                           if Table.Exclusive = False 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
 

Author Comment

by:westy100697
ID: 1358283
Where is that at inprise? FAQ? Anyway will look at this immediately and let you know how it goes. Had a look at it here and it looks like it will do the job. Thanks Zif appreciate the help.

WESTY 8)
(Will most likely award the points as soon as I test it ok)
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1358284
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:westy100697
ID: 1358285
Zif,
    What you just gave me works fine. I will have to automate it though to give me the ability to modify more than one field at a time and will need to be able to do this without actually typing in the field description but this part should be fine now that I know what has been going wrong. Appreciate the help and here are the points and an A grading.
TA heaps.

WESTY
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1358286
Hi westy,

Sorry for not looking earlier to your question, but I guess it slipped through the fingers. Good idea for posting that reminder.

Greetings, Zif.

Happy BDE programming.
0
 

Author Comment

by:westy100697
ID: 1358287
Zif,
    After I got this going I did a little more research as I kept getting " Invalid field descriptor" . I went back to the D3 help for TFields and starting reading then I went and bashed my head on the wall after reading  ".....is a zero based array." I have for the last couple of months been trying on and off to solve this and as it turned out I was initialising my loop with 1 instead of 0. Almost Comical EH!

WESTY (PS. Thanks again)
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1358288
westy, hope your head is ok again ;-). I guess, we all have this thing now and then... some almost everytime ... ;-). See you, Zif.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

706 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