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!

FOR.. ENDFOR Statement

Status
Not open for further replies.

mherz715

Programmer
Apr 4, 2012
17
PH
Hi!
I have a problem regarding on my data.see sample below:
In my table1 I have two fields:
PRODUCT# DESCRIPTION
A516 PRODUCT#1
A516 PRODUCT#2
B200 PRODUCT#3
B200 PRODUCT#4
Now, what I want to do is to tag the data with the same product#. (for report purposes only) Like this:
PRODUCT# DESCRIPTION TAG
A516 PRODUCT#1 1
A516 PRODUCT#2 1
B200 PRODUCT#3 2
B200 PRODUCT#4 2

Is this possible using For.. next loop?? or any suggestion that this data make possible.

Thank in advance.
 
I would do this with some SQL rather than a FOR loop.

Here is a first stab at it. I haven't tested this, but it should give you the general idea.

Code:
* Group the original data
SELECT Product FROM MyTable ;
  GROUP BY Product
  INTO CURSOR ProdGroups

* Add the record number as the tag
SELECT Product, RECNO() AS Tag FROM ProdGroups ;
  INTO CURSOR ProdGroups1

* Combine the original data with the tag
SELECT my.Product, my.Description, gr.Tag ;
  FROM MyTable my JOIN ProdGroups1 gr ON my.Product = gr.Product ;
  INTO CURSOR Result

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 

select * from table1 order by prodno into cursor temp readwrite

alter table temp add tag n(5) && add column tag

locate
lncount = 0

do while not eof()
lncount = lncount + 1
lcprodno = prodno
do while prodno = lcprodno
replace tag with lncount
skip
enddo
enddo



 
>(for report purposes only)

Then add a group into your report, grouping on product# and a report variable counting up +1 with each group change would be your tag number.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top