Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 2nd, 2003, 05:29 PM
Authorized User
 
Join Date: Nov 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default SELECT SUM syntax...

I want a label or text box on a form to display the summed amount of all entries in a quantity column. I've typed out the code and the SQL but can't get it to work. The closest I've gotten is getting the SQL statement to show up in the text box...

I've put the code in the BeforeUpdate and Enter events of the object...

Here's my code:

Private Sub Txt_NewInv_Ware_BeforeUpdate()

Me.Txt_NewInv_Ware.Value =
    "SELECT SUM Location_Quantity AS [Total Warehouse Quantity] " & _
    "FROM Product_Location " & _
    "WHERE Location_ID = 1;"

End Sub



I am, once again, at a loss...
 
Old December 2nd, 2003, 06:09 PM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is your form set-up as continuos form?
just make an unbound textbox in the footer of the form, and enter this in design view

=Sum(Location_Quantity)





Sal
 
Old December 2nd, 2003, 07:03 PM
Authorized User
 
Join Date: Nov 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

that returns an error...

A couple of descrepancies:

I don't know if the form is continuous or not. How do I tell?

Does the text box have to be in the footer area?

I also added the table name to the sum string -->
            =Sum(Product_Quantity.Location_Quantity)
 
Old December 3rd, 2003, 09:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Create a separate simple select query that has Product_Location as the underlying table. Call it, say, qryTotWareQuant. In this query have the fields Location_Quantity and Location_ID as destination fields. Save it.

Then in your procedure, put this in your event procedure:
Code:
Me.Txt_NewInv_Ware.Value = DCount("[Location_Quantity]", _
    "qryTotWareQuant", "[Location_ID] = 1")
    Assuming Location_ID is a number or:
Code:
Me.Txt_NewInv_Ware.Value = DCount("[Location_Quantity]", _
    "qryTotWareQuant", "[Location_ID] = '1'")
    Assuming Location_ID is a string.


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old December 3rd, 2003, 01:21 PM
Authorized User
 
Join Date: Nov 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That doesn't work either...

When I make the query it writes this SQL:

SELECT DISTINCTROW Sum(Product_Location.Location_Quantity) AS [Sum Of Location_Quantity]
FROM Product_Location
GROUP BY Product_Location.Location_ID;

I tried putting the code you wrote in the BeforeUpdate and Enter events. Neither works...
 
Old December 3rd, 2003, 01:25 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Sorry, I didn't make this clear enough. Make the query a simple Select query. Don't try and do any counting, summing, or any math in the query... nothing. Just add the table, the fields that I mentioned, and that's it.

What's happening is that you made the query try and do the work and therefore rendered the DCount function useless. In my instructions, the query just lists everything and the Dcount function does the counting.


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old December 3rd, 2003, 02:16 PM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just enter exactly what I mentioned earlier on a text box on the footer of the form.
To check if the form is continuous or not just view in design, click properties and under format it will say "Continuous Form" on the "Default View" (make sure the complete form is selected to view the properties)

Now, if you want the count of all items use =Count(Location_Quantity).




Sal
 
Old December 14th, 2003, 09:11 PM
Registered User
 
Join Date: Dec 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Anubis! If your name is adam and you once went to nchamber email me at [email protected] immediately. If not then nevermind. Sorry if this post is unrelated to the topic, but I'm just trying to find someone I used to know. :)

 
Old December 15th, 2003, 08:04 PM
Authorized User
 
Join Date: Nov 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

u can use the Dsum function.

I prefer to use the recordset.
Set rst = currentdb.openrecordset(Select * from table where" _
" Location = 1")
While not rst.eof
   sum= sum + rst!Total
   rst.movenext
End While





Similar Threads
Thread Thread Starter Forum Replies Last Post
SELECT SUM Top x CassyFrost Classic ASP Basics 3 October 16th, 2007 08:38 AM
using union all syntax with sum function fdtoo SQL Server 2000 3 July 27th, 2006 02:31 PM
syntax of sum in SQL heghtera SQL Language 1 March 13th, 2006 09:23 AM
syntax of sum in SQL heghtera Access VBA 1 March 10th, 2006 03:17 AM
Within a SELECT do 2 Sum and show only 1 Ford SQL Server 2000 2 November 8th, 2005 01:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.