Print Page | Close Window

How to use loops in formula field on a crystal rep

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2022
Forum Name: Data Connectivity
Forum Discription: How to connect to data sources and export reports
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=15676
Printed Date: 21 Apr 2025 at 8:05pm


Topic: How to use loops in formula field on a crystal rep
Posted By: klps
Subject: How to use loops in formula field on a crystal rep
Date Posted: 20 Feb 2012 at 9:42pm
I need to check all the records in a table.I'm having a foreach loop in my web application. I need to use this loop in crystal report's formula field. I'm new to crystal reports and I have no idea with how to write it in formula field. Can anyone help me?

     
foreach (DataRow dr in ds.Tables[0].Rows)
        {
            type = (dr["type"]).ToString();
            if (type == "c")
            {
                c_total = c_total + Convert.ToDouble(dr["amount"]);
            }
            else if (type == "d")
            {
                d_total = d_total + Convert.ToDouble(dr["amount"]);
            }
        }



Replies:
Posted By: rkrowland
Date Posted: 21 Feb 2012 at 12:18am

I'd assume you do this to get a balance of debits and credits after individual transactions (records)?

If so I'd just create a formula field as follows and then create a running total on this field;
 
if {table.typefield} = "c" then -{table.amountfield} else
if {table.typefield} = "d" then {table.amountfield}
 
You may need to change the "-" position - You should be able to create a running total on this field on each record resetting at customer/supplier or whatever you need.
 
If I'm misunderstanding what you're trying to accomplish let me know.
 
Regards,
Ryan.



Print Page | Close Window