Extra Zeros When Adding Variable Values

sherlocked

Registered User.
Local time
Today, 03:25
Joined
Sep 22, 2014
Messages
125
Hello experts,

I have a report that pulls data based on several SELECT statements into recordsets, counts the records in the sets and saves that count in INTEGER variables.

I am attempting to design an unbound control on my report that adds up the values of these variables to display a total count of records. I do this by dimming a variable to hold the total (I have tried this is INTEGER, DOUBLE and LONG) and then setting the value of the unbound text box on my report to this variable.

For some reason, the field is displaying extraneous zeroes when my report loads. The number should be "2" but it is displaying as "200". I have the unbound text box format set to General Number and the decimal place at 0.

I have stepped through my code and verified that the values of the variables I'm trying to add are actually there and not null or some other value that might screw up my math.

Any thoughts on what may be causing this? I've exhausted my (fairly limited) VBA knowledge trying to suss this out.

My code looks something like this:

Code:
Dim Variable1 as Integer, Variable2 as Integer, Variable3 as Integer
Dim rs1 as dao.recordset, rs2 as dao.recordset, rs3 as dao.recordset
Dim Mytotal as Integer

Set rs1 = CurrentDb.OpenRecordset("SELECT STATEMENT")
Set rs2 = CurrentDb.OpenRecordset("SELECT STATEMENT")
Set rs3 = CurrentDb.OpenRecordset("SELECT STATEMENT")

Variable1 = rs1.recordcount
Variable2 = rs2.recordcount
Variable3 = rs3.recordcount

Mytotal = Variable1 + Variable2 + Variable3

Thanks in advance! :D
 
You are concatenating the answers as strings, even though you don't think you are.

so 2 + 0 + 0 = "200"

Try using

MyTotal = Val(variable1) + Val(variable2) + Val(variable2)
 
Thank you for this, but I'm afraid when I do this I get "2000" now! There are many more variables involved than what I have listed in my example.

Other thoughts?
 
All,

I was able to figure this out. I was trying to add up so many variables that I had four lines of code and was joining them with "&" symbols - the report was treating each line as a separate calculation. I broke down the sets into smaller variables, added those up and got the "2" total I was expecting.

Thanks for helping me work through it! :D
 

Users who are viewing this thread

Back
Top Bottom