Solved

For loop in Crystal reports - printing issue

Posted on 2009-06-30
2
1,459 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

There have always been a lot of questions related to when Crystal Reports evaluates report components (such as formulas, summaries, cross-tabs, charts, to name a few examples). Crystal Reports uses a two-pass reporting process to provide greater …
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 this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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