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

Append to new Cursor Help

Status
Not open for further replies.

txgeekgirl1

Programmer
Sep 10, 2009
85
US
I need to append two fields into a created cursor. What am I missing here?

CREATE CURSOR MySeq_(SeqDate c(6), seqOrder n(2))


FOR i = MONTH(ldSDate) TO 12
lnYear = YEAR(ldSDate)
APPEND MySeq_
REPLACE MySeq_.SeqDate WITH STR(lnYear)+STR(i) ;
AND MySeq_.seqOrder WITH lnCnt+1
NEXT i

IF YEAR(ldSDate) <> YEAR(ldEDate) THEN
FOR i = 01 TO MONTH(ldEDate)
lnYear = YEAR(ldEDate)
APPEND MySeq_
REPLACE MySeq_.SeqDate WITH STR(lnYear)+STR(i) ;
AND MySeq_.seqOrder WITH lnCnt+1
NEXT i
ENDIF

SELECT MySeq_
browse
 
First, try using APPEND BLANK instead of append.

Next, if you're going to use a single REPLACE, separate the fields with a comma:
REPLACE somefield WITH somevalue, anotherfield WITH anothervalue
Or just use more than one REPLACE:

REPLACE somefield WITH somevalue
REPLACE anotherfield WITH anothervalue


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
You have a series of errors there some of which would not cause VFP to show up an error message:

1) It would be:
Append Blank

2) Replace command would separate the fields with a comma rather than 'AND'.

3) Str() result is fixed to 10 in length and the result is right aligned. Your SeqDate OTOH is 6 characters. In effect, that means if you had it written correct in items 1 and 2 then seqDate would be empty.

4) The variables are not prefixed with m. That:
a) Can cause field name/variable name clashes
b) Since VFP7 causes a slowdown within loops (if the loop count is high enough that slowdown can be really high - there are a few factors contributing to it, not talking for your loop but in general).

5) Declare your variables as local and especially the loop variable i.

6) Append blank and then replace is not 100% reliable unless it is a single user application. Instead prefer Insert into (SQL) syntax that does appending the record with values in a single step.

Initial value of m.lnCnt is not in code and what your real intent is with the value in seqDate is not clear, by guessing I would write it as:

Code:
Create Cursor MySeq_(SeqDate c(6), seqOrder N(2))

Local i, lnYear
lnYear = Year(m.ldSDate)
For i = Month(m.ldSDate) To 12
  Insert Into MySeq_ ;
    (SeqDate, seqOrder) ;
    Values ;
    (Padl(m.lnYear,4)+Padl(m.i), m.lnCnt+1)
Endfor


If m.lnYear <> Year(m.ldEDate)
  lnYear = Year(m.ldEDate)
  For i = 1 To Month(m.ldEDate)
    Insert Into MySeq_ ;
      (SeqDate, seqOrder) ;
      Values ;
      (Padl(m.lnYear,4)+Padl(m.i), m.lnCnt+1)
  Endfor
Endif

Select MySeq_
Browse


Cetin Basoz
MS Foxpro MVP, MCP
 
Opps typo:
Code:
Create Cursor MySeq_(SeqDate c(6), seqOrder N(2))

Local i, lnYear
lnYear = Year(m.ldSDate)
For i = Month(m.ldSDate) To 12
  Insert Into MySeq_ ;
    (SeqDate, seqOrder) ;
    Values ;
    (Padl(m.lnYear,4)+Padl(m.i,2,'0'), m.lnCnt+1)
Endfor


If m.lnYear <> Year(m.ldEDate)
  lnYear = Year(m.ldEDate)
  For i = 1 To Month(m.ldEDate)
    Insert Into MySeq_ ;
      (SeqDate, seqOrder) ;
      Values ;
      (Padl(m.lnYear,4)+Padl(m.i,2,'0'), m.lnCnt+1)
  Endfor
Endif

Select MySeq_
Browse

Cetin Basoz
MS Foxpro MVP, MCP
 
Thank you cbasoz - I declare all variables at the top of the prg - old school style - because most of out developers are old school.

I am still having issues getting the Append to work. It shouldn't be this hard. I can get whole spreadsheets to append - but not parts of a stupid date.

I went to the lengths of declaring a new Var named lcMyStr to be lcYear = ALLTRIM(STR(YEAR(ldSDate)))
i = ALLTRIM(STR(i))
lcMyStr = lcYear + i

I have a wait window showing the correct value.

It tells me Not a Numeric Expression.

I sub'ed "abcdef" and 123456 still a no go.

Having written the rest of the prg and report design, I scheduled time with my boss to help me troubleshoot the append issue for this afternoon.
 
Here is what I ended up with... follows along with what CBasoz said on a FoxPro Wiki post.

Code:
Create Cursor MySeq_(SeqDate c(6), seqOrder N(2,0))
	
	FOR i = MONTH(ldSDate) TO 12
		lcMyStr = ALLTRIM(STR(YEAR(ldSDate))) + RIGHT("0" + ALLTRIM(STR(i)),2)
		lnCnt = lnCnt + 1
		INSERT INTO MySeq_ (SeqDate, SeqOrder) ;
			VALUES(lcMyStr, lnCnt)
	NEXT i        
		
	IF YEAR(ldSDate) <> YEAR(ldEDate) THEN    
		FOR i = 01 TO MONTH(ldEDate)    
			lcMyStr = ALLTRIM(STR(YEAR(ldEDate))) + RIGHT("0" + ALLTRIM(STR(i)),2)
			lnCnt = lnCnt + 1
			INSERT INTO MySeq_ (SeqDate, SeqOrder) ;
				VALUES(lcMyStr, lnCnt)
		NEXT i     
	ENDIF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top