Solved

For loop in Crystal reports - printing issue

Posted on 2009-06-30
2
1,449 Views
Last Modified: 2012-05-07
Hi,

I have a formula field, in basic syntax, that uses a for loop.  The contents of the formula section should be printed the number of times as (i), the number of times it loops.

However, it loops 5, and only prints the last one?

Here is my code:
Dim i As number
Dim dval As Double
Dim deprec As Double
Dim d_cost as double
dim i_term as number

d_cost = {ITEMUNITOFMEASURE.UnitCost}
i_term = {ITEMUNITOFMEASURE.Lifespan}

dval = d_cost
For i = 1 To i_term
            deprec = d_cost / CDbl(i_term)
            dval = dval - deprec
Next i        
formula = "End of Year" &  i  & " Depreciation:" & deprec & "Current Value:" & dval


Here is the output:
End of Year5.00 Depreciation: 2,189.72 Current Value:240.88

The output that I want is:
End of Year1.00 Depreciation: 2,189.72 Current Value:8,999.76
End of Year2.00 Depreciation: 2,189.72 Current Value:6,810.04
End of Year3.00 Depreciation: 2,189.72 Current Value:4,620.32
End of Year4.00 Depreciation: 2,189.72 Current Value:2,430.60
End of Year5.00 Depreciation: 2,189.72 Current Value:240.88

Thank you.
0
Comment
Question by:Lalla Belle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
ID: 24743966
your 'formula= ' command is outside of the loop so you will only get the last value.
you need a
Dim Result as string

plus...


For i = 1 To i_term
            deprec = d_cost / CDbl(i_term)
            dval = dval - deprec
              result = result & "End of Year" &  i  & " Depreciation:" & deprec & "Current Value:" & dval & chr(13)
Next i        
formula = result
0
 

Author Closing Comment

by:Lalla Belle
ID: 31598284
Thank you it worked perfectly!
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I hate sub reports and always consider them the last resort in any reporting solution.  The negative effect on performance and maintainability is just not worth the easy ride they give the report writer.  Nine times out of ten reporting requirements…
Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question