Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can one add a field to a table in a .prg

Status
Not open for further replies.

cstella23

Technical User
Oct 4, 2002
5
0
0
US
Is there a way to add a field to a table (in FoxPro 2.6) in a prg file, without doing it manually? is it something close to:

MODIFY TABLE xxxx ;
(field1 c(5))

 
HI Cstella,

Method1...
**************************
myNewField1 = SPACE(15)
myNewField2 = 000000000.00

SELECT *, myNewField1 AS AltName, myNewField2 AS Amount2 ;
FROM oldTable INTO DBF NewTable
INDEX ON .... whatever..
CLOSE DATABASE

Now that the NeTable is available..
DELETE FILE "oldTable.dbf"
DELETE FILE "oldTable.CDX"
DELETE FILE "oldTable.FPT"

If required.. REname NewTable to the same name as old table.


Method2
*********
USE oldTable
COPT STRU EXTENDED TO temp
USE temp
APPEND BLANK
REPLACE fieldname etc etc...
and now use the temp to create the NewTable .
Look in the help file on COPY TO STRU EXTE

:)
ramani :)
(Subramanian.G),FoxAcc, ramani_g@yahoo.com
 
There are a number of ways to do this.

Here are two possibilities:

Method 1:
Look at Foxpro's Help AFIELDS()

* -- Collect Field info from SourceTable into Array
USE SrcTable EXCLUSIVE
SELECT SrcTable
=AFIELDS(SrcFldArr) && SrcFldArr is array
USE && close SrcTable

* -- Add array elements for new fields ---
m.nArrRows = ALEN(SrcFldArr,1)
DIMENSION SrcFldArr (m.nArrRows + 1,4) && to add 1 field
SrcFldArr(m.nArrRows+1,1) = "NEWFLD" && new field name
SrcFldArr(m.nArrRows+1,2) = "N" && new field type
SrcFldArr(m.nArrRows+1,3) = 8 && new field integ size
SrcFldArr(m.nArrRows+1,4) = 3 && new field decimal size

* -- Create new table structure
CREATE TABLE tmptable FROM ARRAY SrcFldArr
SELECT tmptable

* -- Read in records from SrcTable
APPEND FROM SrcTable

* -- Move new structure & records into SrcTable
COPY TO SrcTable
USE && close TmpTable
ERASE TmpTable && clean house

Method 2:
Use SELECT - SQL

SELECT SrcTable.*,;
SPACE(10) AS newfld1,;
99999 AS newfld2;
FROM SrcTable;
INTO TABLE TmpTable

SELECT SrcTable
USE && close SrcTable

* --- Copy new structure and records into SrcTable
SELECT TmpTable
COPY TO SrcTable
ERASE TmpTable

NOTE: You will need EXCLUSIVE access to the table that you are wishing to change since the COPY TO ... command will not work if the table is in USE.

Good Luck,

JRB-Bldr
VisionQuest Consulting
Business Analyst & CIO Consulting Services
CIOServices@yahoo.com
 
That is exactly what I needed, thank you 2 very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top