polachan


Sir

How to get the dbf table's Field Name and Field Type and Field Size . Please help me by providing a program tips in foxpro to find out name , type,size of each field in a table

For example

I have a table employee having the following field

code c(5)

Name c(10)

dept c(30)

Salary n(10,2)

remark memo(50)

I want to create a duplicate file and to define the field dynamically But if the orginal file have memo field , I have to define as charector field for the seconed file.

After executing the program the seconed file will be created with the following structure

code c(5)

Name c(10)

dept c(30)

Salary n(10,2)

remark c(50)

Please help me

Regards

POL





Re: How to get the dbf table's Field Name and Field Type and Field Size

CetinBasoz


There are multiple ways to get that info in VFP and what would you choose depends on what you want to do. Here is one of the ways:

Code Snippet

use myTable

for ix = 1 to afields(laFieldInfo)

laFieldInfo[m.ix,1], laFieldInfo[m.ix,2], laFieldInfo[m.ix,3], laFieldInfo[m.ix,4]

endfor






Re: How to get the dbf table's Field Name and Field Type and Field Size

hangover

Check out the AFIELDS function, e.g.

LOCAL ARRAY laStructure [1]

lnNoOfFields = AFIELDS (laStructure, mytable)

FOR lnField = 1 TO lnNoOfFields

lcFieldName = laStructure [lnField, 1]

lcFieldType = laStructure [lnField, 2]

lnWidth = laStructure [lnField, 3]

lnDecimals = laStructure [lnField, 4]

ENDFOR







Re: How to get the dbf table's Field Name and Field Type and Field Size

polachan

Dear all

Thank u for your immediate reply . But I forgot to add before my requirement clearly . So I changed my query and please reconsider






Re: How to get the dbf table's Field Name and Field Type and Field Size

hangover

Try this

Code Snippet

LOCAL lnNoOfFields, lnField

LOCAL ARRAY laStructure [1]

lnNoOfFields = AFIELDS (laStructure, mytable)

FOR lnField = 1 TO lnNoOfFields

IF laStructure [lnField, 2] = "M"

laStructure [lnField, 2] = "C"

laStructure [lnField, 3] = 50

ENDIF

ENDFOR

CREATE TABLE mytable2 FROM ARRAY laStructure

APPEND FROM DBF ("mytable")






Re: How to get the dbf table's Field Name and Field Type and Field Size

CetinBasoz

Ooops you edited your question in between! For memo field size is always 4 how come you have 50 there It's not dynamic, it's a hardcoded value. You may simply do:

select code,name,dept,salary, left(remark,50) as remark from myTable into cursor newTable readwrite

Or:

Code Snippet

dimension aNewStruct(afield(laFieldInfo),4)

for ix=1 to aNewSruct(laFieldInfo,1)

for jx = 1 to 4

aNewStruct[m.ix,m.jx] = laFieldInfo[m.ix,m.jx]

endfor

if aNewStruct[m.ix,2] = 'M'

aNewStruct[m.ix,2] = 'C'

aNewStruct[m.ix,3] = 50

endif

endfor

create table myNewTable from array aNewStruct

PS: Be carefull with tables that have long field names.




Re: How to get the dbf table's Field Name and Field Type and Field Size

polachan

Sir Thank u very much

Please let me know how can I give Mytable .That is

lnNoOfFields = AFIELDS (lastructure, mytable) where is mytable is declared

Please help me






Re: How to get the dbf table's Field Name and Field Type and Field Size

Naomi Nosonovsky

myTable is the actual alias of your table.

It should be opened first.

E.g.

use c:\myDataDir\MyTable.dbf alias Mytable in 0 shared && Here you would use the actual table path and name and any alias you want

lnNoFields = afields(laFields,'myTable')





Re: How to get the dbf table's Field Name and Field Type and Field Size

polachan

Sir,

select a

use salary alias salary

lnNoOfFields = AFIELDS (lastructure, salary)

But error is coming






Re: How to get the dbf table's Field Name and Field Type and Field Size

Naomi Nosonovsky

Please read carefully what I posted.

First of all, don't use

SELECT a

Always use SELECT 0 to find the next available select area.

Secondly, in your code you don't even need the second parameter in aFields function.

Anyway, it should be

lnNoOfFields = afields(laStructure, 'Salary') && Note that I put salary in quotes

Also these are equavalent:

lnNoOfFields = afields(laStructure, [Salary])

OR

lnNoOfFields = afields(laStructure, "Salary")

OR

lcAlias = [Salary]

lnNoOfFields = afields(laStructure, m.lcAlias)





Re: How to get the dbf table's Field Name and Field Type and Field Size

polachan

Dear all

Very much Thank u for ur reply to my queries. I got the answer from ur reply . Thank u once again very much

Regards

POL