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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execution by going field to field 1

Status
Not open for further replies.

buddyrich2

Technical User
Apr 12, 2006
87
US
I know how to go down a table and execute a command for each record. My question is, that if I have only one record but many fields, is there anyway I can automatically go field to field until the end and execute a command on each (memo) field?

Thanks.
 
The FCOUNT() function will tell you how many fields you've got, FIELD() will return you the name of a field, and TYPE() will tell you the data type. So you can find memo fields by looking for TYPE(FIELD(n))="M"

Geoff Franklin
 
You can probably do it a little faster by using AFIELDS() to populate an array with the table's structure and then go through that to identify the fields you need.

Tamar
 
I dont think there is a performance improvement with AFIELDS() as you are still checking the #2 element of the array for a "M". (memo) also to much bagage, 1 element as opposed to 18.

Geoff's method is less complicated. (IMO)

Buddyrich:
Code:
(Geoff's method)
select <<table>>
for x = 1 to fcount()
 if vartype(field(x)) = "M"
   *** can use Type() instead of Vartype() 
   *do something
 endif
endfor
 
The benefit to afields() is that data type, width and decimals are included in the array and are easily available in the 'do something' part if you need them.
 
Here's an example of how knowing more about the structure might be helpful.

Code:
CLEAR 

CREATE TABLE test (c1 c(1), n1 n(1,0), m1 m, c2 c(2), m2 m, n2 n(3,1))

FOR i = 1 TO AFIELDS(aa)
  DO CASE 
    CASE aa[i,2]="M"
    ?"Memo field: " + aa[i,1]
    
    CASE aa[i,2]="N" and aa[i,4]=1
      ?"Only one decimal for: " + aa[i,1] + ". Increasing width and decimal by 1"
      ALTER TABLE DBF() ALTER COLUMN (aa[i,1]) n(1+aa[i,3],1+aa[i,4])
    
    OTHERWISE 
      *nothing
  ENDCASE 
ENDFOR
 
Baltman: You are right and Afields() is great when you do not know the structure of the table. BuddyRich2 does, only memos

BuddyRich2: Glad I could help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top