Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 Reporting Services Development
 Textbox Calculation again

Author  Topic 

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-01-13 : 16:17:57
I'm desperately trying to add two text boxes together, but it seems harder than what I thought. If it's of the same field and dataset, then I can do it easily, but the problem is that I have a field from the current dataset and another field from within a subreport. All I'm trying to do is just add Textbox1 + Textbox2 = Textbox3. The only way I think might be possible is to use Custom Code, which is kind of ridiculous.

What might be wrong with this code?

Public Function GrossMargin(ByVal Sale_Amount As Integer, ByVal Total_Cost As Integer) As String
Dim Gross As String
Gross = " "
Gross = Sale_Amount - Total_Cost
Return Gross
End Function

Call function:
=Code.GrossMargin(ReportItems!Sale_Amount.value, ReportItems!Total_Cost.value)

This seems to run the report, but I'm getting "#Error". I think it might have to do with my custom code. Sale_Amount is a field from the current dataset, but Total_Cost is a field within the Subreport. Not sure how RS know that it's from the Subreport. That might be where the problem is.

Can anyone check to see if my custom code makes sense?

Thanks in advance.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-01-13 : 16:22:57
I don't think that you can refer to a field from the subreport inside the main report.

I think your code should be:

Public Function GrossMargin(ByVal Sale_Amount As Integer, ByVal Total_Cost As Integer) As String
GrossMargin = " "
GrossMargin = Sale_Amount - Total_Cost
End Function

Then:
=Code.GrossMargin(Fields!Sale_Amount.value, Fields!Total_Cost.value)

I don't think that you are going to be able to use Total_Cost though in the main report. The main report only knows what parameters to pass the subreport. I could be wrong though. But I don't see any option in the main report where I can select fields from the subreport.

Tara
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-01-13 : 16:23:46
quote:
Originally posted by chriskhan2000

I'm desperately trying to add two text boxes together, but it seems harder than what I thought. If it's of the same field and dataset, then I can do it easily, but the problem is that I have a field from the current dataset and another field from within a subreport. All I'm trying to do is just add Textbox1 + Textbox2 = Textbox3. The only way I think might be possible is to use Custom Code, which is kind of ridiculous.

What might be wrong with this code?

Public Function GrossMargin(ByVal Sale_Amount As Integer, ByVal Total_Cost As Integer) As String
Dim Gross As String
Gross = " "
Gross = Sale_Amount - Total_Cost
Return Gross
End Function

Call function:
=Code.GrossMargin(ReportItems!Sale_Amount.value, ReportItems!Total_Cost.value)

This seems to run the report, but I'm getting "#Error". I think it might have to do with my custom code. Sale_Amount is a field from the current dataset, but Total_Cost is a field within the Subreport. Not sure how RS know that it's from the Subreport. That might be where the problem is.

Can anyone check to see if my custom code makes sense?

Thanks in advance.



Make sure the fields have a default of 0.
To avoid any of this create calculated fields on the bottom left hand side of RS, you should be able to add a calculated field. In that calculated field perform the operation.

Jon



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]


Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-01-13 : 16:39:23
You may be right Tara. Microsoft should be it so the user can add Textbox1 to Textbox2 without having the fields to be from the same dataset. I just can't understand why they can't implement RS to be able to add like this: Textbox1 + Textbox2 = Textbox3.

Jon, I'm not following. How is it possible to create the calculation in the calculated field when the Total_Cost field is within a subreport? When I go to create the calculated field, all I'm seeing is just the fields for the current dataset. Is there a way to reference another field from another dataset or subreport?

Just out of curiosity, has anyone ever try to do some calculation with 2 texboxes before and they are not from the same dataset?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-01-13 : 16:44:05
quote:
Originally posted by chriskhan2000

Just out of curiosity, has anyone ever try to do some calculation with 2 texboxes before and they are not from the same dataset?



I haven't. I also don't think that you will be able to as the other dataset is out of scope. You may want to consider handling this type of thing at the stored procedure level.

Tara
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-01-13 : 16:56:59
Tara, when I removed the Total_Cost, and just put a numeric value in replace, it seems to be working fine. So I guess it is the Total_Cost field that is giving me the error. I don't know if there's a way to reference that from another dataset or subreport.
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-01-17 : 12:51:21
Problem Solved!!!

Just in case if anyone is having same issue. I got it to work thanks to the help of a user from another forum.

Here's how I manage to get it to work. The workaround is to use the mainreport, the field that I want to calculate, as a parameter. Then in the subreport do the caculation there.

Example.

Parameters!Textbox1.value + ReportItems!Textbox2.value

THe parameter is from the main report and the Reportitems is from the subreport. It's not what I want to have to go through to add two textbox, but it's a workable solution.
Go to Top of Page
   

- Advertisement -