Click to See Complete Forum and Search --> : How To Add Column TO DBF File


Vincent
03-16-2000, 01:10 PM
I'm a programmer with very limited skills in using FoxPro. I am
using version 2.6. I need to write some code that will add a column
to an existing DBF file and how will this affect the DBF Indexes.
I know about using the online version to modify the DBF file. I want
to create a executables in which I can send to other places to modify
their standalone computers that have this particular DBF file.

Thank You

Rick Bean
03-16-2000, 02:58 PM
Vincent,
I've used four different techniques, depending on the complexity and nature
of the .DBF change. They all essentially require creating a new file and
transferring the data over and recreating the indexes.
1) The simplest is to create the new file using an SQL SELECT, and adding in
the new field(s) as a default. The only limitation is that you can't create
a new memo field this way. For example to add a new character field:
SELECT *, SPACE(3) as SPECCODE ;
FROM REFERRAL ;
INTO TABLE REFN.DBF

Where the old table is Referral, the new table is Refn and the new field is
SPECCODE C(3) with a default value of all spaces. You would then close both
tables, rename the old to whatever (Referral.DBO e.g. - don't forget to
rename the .CDX if it has an index and the .FPT if it has any memo fields).
Next rename Refn.DBF to Referral.DBF (don't forget the .FPT if it exists),
and then recreate the indexes on the file. The reason this is simplest, is
that you don't have to copy the data in a separate step.

2) The next is to simply create a whole new table using the CREATE TABLE
command (with the new field(s) added) and then APPEND into it from the old
table. e.g.
** Add MCTYPE field
**
CREATE DBF REFERRAN (;
REF_ID C(3),;
REFERLAST C(15),;
REFERFIRST C(10),;
REFERSPEC C(3),;
REFERLIC C(10),;
REFERUPIN C(6),;
REFERPPO C(11),;
REFERMCNUM C(10),;
REFERHMO C(10),;
INACTVDATE D,;
SPECCODE C(3),;
MCTYPE C(3);
)
APPEND FROM REFERRAL

(You'd have to do the renaming and reindexing here too.)

3) The next is using the COPY STRUCTURE EXTENDED command

USE REFERRAL
COPY STRUCTURE EXTENDED TO work
USE work
APPEND BLANK
REPLACE field_name WITH 'MCTYPE', field_type WITH 'C', field_len WITH 3
USE
CREATE REFERRAN FROM work
USE REFERRAN
APPEND FROM REFERRAL

(You'd have to do the renaming and reindexing here too, along with deleting
WORK.DBF.)

4) The last, is using the AFIELDS() function:

USE REFERRAL
gnFieldcount = AFIELDS(gaMyArray)
DIMENSION gaMyArray[gnFieldcount+1, 4]
gaMyArray[gnFieldcount+1, 1] = 'MCTYPE'
gaMyArray[gnFieldcount+1, 2] = 'C'
gaMyArray[gnFieldcount+1, 3] = 3
gaMyArray[gnFieldcount+1, 4] = 0
CREATE DBF REFERRAN FROM ARRAY gaMyArray
USE REFERRAN
APPEND FROM REFERRAL

(You'd have to do the renaming and reindexing here too.)

I hope I haven't given you too much information - just use the one that
makes the most sense to you.

Rick

"Vincent" <vhollida@email.usps.gov> wrote in message
news:38d1158c$1@news.devx.com...
>
> I'm a programmer with very limited skills in using FoxPro. I am
> using version 2.6. I need to write some code that will add a column
> to an existing DBF file and how will this affect the DBF Indexes.
> I know about using the online version to modify the DBF file. I want
> to create a executables in which I can send to other places to modify
> their standalone computers that have this particular DBF file.
>
> Thank You