View Full Version : SUM statement


JonyBravo
07-06-2006, 10:05 AM
On the following code I want to attribute the value of the sum([price]) to an variable called txtFullPrice:

Is there anybody that could help me please.

Dim dbs As Database, rst As Recordset
Dim strcriteria As String

'return reference to current database
Set dbs = CurrentDb

'Define serach criteria
strcriteria = "SELECT SUM([Price]) FROM tblRBS WHERE [USERID]='" & FOSUsername & "'"

'Create a dynadet-type Recordset object based on tblUser table
Set rst = dbs.OpenRecordset(strcriteria)

rst.Movelast

*******

'close connection on tblUser table
rst.Close
Set dbs = Nothing

This Forum have been helping me to improve my skills a lot. Thanks to everyone.

Jony Bravo

Matt Greatorex
07-06-2006, 10:20 AM
Firstly, is txtPrice
1)a string variable (based on the txt part),
2)a numeric variable or
3) a text box on a form/report?
I know you said variable, but I wanted to double-check.

If it's a string, you can't set it to be a sum.

Assuming it's a number, try:

Dim txtPrice as Integer
txtPrice = DSum("[Price]","tblRBS","[USERID]='" & FOSUsername & "'")

Assuming it's a text box, try

[forms]![formname]![txtPrice] = DSum("[Price]","tblRBS","[USERID]='" & FOSUsername & "'")

Hope that's of some use.

JonyBravo
07-06-2006, 03:03 PM
Hy!
Thanks to help. DO you mean something like it?

Dim dbs As Database, rst As Recordset
Dim strcriteria As String

'return reference to current database
Set dbs = CurrentDb

'Define serach criteria
strcriteria = "SELECT SUM([Price]) FROM tblRBS WHERE [USERID]='" & FOSUsername & "'"

'Create a dynadet-type Recordset object based on tblUser table
Set rst = dbs.OpenRecordset(strcriteria)

rst.Movelast

[forms]![formname]![txtPrice] = DSum("[Price]","tblRBS","[USERID]='" & FOSUsername & "'")


'close connection on tblUser table
rst.Close
Set dbs = Nothing

Matt Greatorex
07-07-2006, 05:50 AM
Hy!

[forms]![formname]![txtPrice] = DSum("[Price]","tblRBS","[USERID]='" & FOSUsername & "'")



Just to put that value into the textbox you named, this line should be enough on it's own.

JonyBravo
07-07-2006, 12:04 PM
Hy!
This worked perfect. Thanks very much.
:p

Jony Bravo

Matt Greatorex
07-07-2006, 12:44 PM
Thanks for letting me know.

You're very welcome.