Simple sum in a calculated control MS-access - ms-access

Trying to get the sum of three fields and enter it in a fourth text box on the same report. For a particular report,
empnose =78
empright=555
empleft= 565
The total text box should be 1198
using the expression
=Sum([EmpNose] And [EmpRight] And [EmpLeft])
the result is -4
using the expression
=Sum([EmpNose]+[EmpLeft]+[EmpRight])
the result is 226514940
using the expression
=([EmpNose]+[EmpLeft]+[EmpRight])
the result is 78555565 (the three values concatenated)
What is the correct syntax?

If you want to add up the values of the current record only, Sum() is wrong, because it sums over all records.
=([EmpNose]+[EmpLeft]+[EmpRight])
should be correct. If it concatenates the values it seems your fields are text fields instead of numbers?
If you can't change the datatypes to numbers, you can try
=(Val([EmpNose]) + Val([EmpLeft]) + Val([EmpRight]))
The Val() function tries to convert a string to a number.

Related

Comparing multiple fields in two datasets to return a 3rd value

I am in report builder and I have my primary dataset that is from a SQL database, I also then created a second dataset (enter data). I need to compare 2 fields from each dataset to retrieve the correct value from the 2nd dataset and populate a column on my report. I have tried the IIF statements and Lookup statements but I keep getting the error "report item expressions can only refer to fields within the current dataset".
I have a attached a screenshot of what I am trying to do....
The IIF statement I tried to use.. If Acctnum and prodid = each other return IncodeNumber
=IIF((Fields!AcctNum.Value=Fields!AcctNum.Value, "IncodeAccount") AND
(Fields!ProdId.Value =Fields!ProdId.Value, "IncodeAccount")),(Fields!IncodeNumber.Value, "IncodeAccount"),"True")
See code in my problem.
You need to use LOOKUP(). The problem with LOOKUP() is that is can only compare a single value from each dataset. However, we can easily get around this issue by concatenating the two values you need to compare.
Note: This assumes the expression will be in a tablix that is bound to your first dataset and that IncodeAccount is your second dataset - the values you want to lookup. If this is not the case just adjust the expression accordingly
So for you, you probably need to do something like this..
=LOOKUP(
Fields!AcctNum.Value & "||" & Fields!ProdId.Value,
Fields!AcctNum.Value & "||" & Fields!ProdId.Value,
Fields!IncodeNumber.Value,
"IncodeAccount"
)
I've used two pipe symbols to join the values to avoid incorrect matches being found. e.g. Account 123 and product ID 4567 would incorrectly match to Account 1234 and product ID 567 as they would both be 1234567 when joined. By using the || the match would be 123||4567 and 1234||567 respectively.
You may need to convert the values to string using CStr()
Alternative approach
If you are going to do this 'join' multiple times in the same dataset then you could add a calculated column to the dataset that concatenates the two columns. Then you can use this single field in the lookup which will make things a little simpler.
Or, you could do this concatenation in a database view which would make things even easier.

MS Access Expression That Includes Dynamic Field Names

I have a crosstab query which returns results based on consumer demand for a bunch of material numbers. The material numbers become my field names in the crosstab query, and later the values from those fields are displayed in a form.
In the form, I display the value in a textbox. There are a couple of these textboxes where I need to sum the total of two or more values from these fields. Not a big deal it's a simple expression. For example (in the Control Source property): =[H123457] + [H123456].
This works well UNTIL there is no demand for a particular material number. In this case, the field doesn't show up in the crosstab query and I'm left trying to sum two fields where one doesn't exist.
I've tried IIf(IsError([H123456]), 0, [H123456]), Null expressions, Nz function, etc but cannot figure out how to dynamically solve the #Name issue that ends up populating the text box.
Essentially what I want is for a 0 value for the field that doesn't exist, so I can add it to the value where the field DOES exist - is this possible?
Regards!
June7 provided the answer in the allenbrowne.com link. Essentially, you need to add all of the possible field names to the Column Headings property in the crosstab query property window. Then it's a simple matter of adding an Nz() function to handle null values.
Thanks June7!

How to Sum value of fields in a ms access form

I have a form where in need to calculate sum of multiple text boxes.
example: i have two text boxes A=10.15 and B= 15.60. I want to sum their values in text box 'c'. I tried using the sum function but results in '#Error' and '+' which appends the two values '10.1515.60'.
Any Suggestions???
Thank you
Tried this =Val[TextBoxA.value]+Val[TextBoxB.value]
it works, when I try to sum two textboxes in another textbox, at first Access concatenated the values but with this formula access let you sum both values.
Set the Control Source of textbox C (without the quotes).
'=[TextBoxA]+[TextBoxB]'
Make sure the textbox format is Number. Leaving it blank concatenates the values.
The SUM function is for totaling the values in the same field, across multiple records.
To calculate the total of two textboxes, set the Control Source property of textbox C to the following (include the = sign)
= NZ([A],0) + NZ([B],0)
The NZ function gracefully handles NULLS by changing them to 0.
Note that textbox C will be unbound, so the sum will not be stored in your table (but it is not a good practice to store calculations, so that should be OK).

SSRS: Get values from a particular row of DataSet?

My dataset currently has 12 rows of data. Each representing data for a month. I would like to have variance of a column between to rows, the rows being last & last but one i.e., latest month and previous month's data.
It could have been simple if I were to work on tablix but thats not the case. I want those values for a textbox.
Any ideas on it anyone?
I hope you are using SSRS 2008R2:
R2 introduced the Lookup function that is perfect for this scenario.
=Lookup( Fields!ProductUID.Value ,Fields!ProductID.Value,Fields!Price.Value,"PriceDataSet")
The Lookup function above will evaluate the first parameter ("Fields!ProductUID.Value") in the current dataset, then look for a matching value in the field specified in the second parameter ("Fields!ProductID.Value") in the dataset specified in the fourth parameter. The value of the third parameter is then evaluated in that row of the dataset and returned.
A little convoluted, but very helpful.
In your case, you can use this in a textbox with a calculated a static number:
=Lookup(
Month(DateAdd(DateInterval.Month, -1, GetDate())),
Fields!MonthID.Value,
Fields!Name.Value,
"DataSet1")
This should calculate a number for last month, then look for a match in DataSet1.
In this example I have a tablix with Statecode and name as below
enter image description here
Suppose you want to display the name of state of CA, write an expression as -
=Lookup(
"CA" ,
Fields!StateCode.Value,
Fields!StateName.Value,
"ReportData"
)
This will return 'California' in the text box
I ran across this post while trying to solve a similar problem but with columns of double data type. Not sure why but SSRS did not want to return my first row using LOOKUP in combination with ROW_NUMBER in SQL(If someone can solve that all the better). I ended up using a SUM(IIF) instead. Hopefully, this is useful for someone else.
=Sum(IIF(Fields!RowNum.Value=1,CDBL(Fields!MyNumericColumn.Value),CDBL(0)))
Note: If SSRS complains about data types, just cast both parts of the IIF to the desired data type.

how do i make addition of two fields in ssrs expression?

I have generated one ssrs report
now i have two fields having values
1st field value is =Fields!FirstNO.Value
2nd field value is =Fields!SecondNO.Value
now i have one field average and i need to write expression like
average = (Fields!FirstNO.Value+Fields!SecondNO.Value) / (Fields!SecondNO.Value)
how can i write above in expression?? directly as i shown or any other syntax is there please help?
Are you sure these fields are numeric? Maybe try convert to numeric, like =cint(Fields!FirstNO.Value)