Query/VBA to Sum the records

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi, i need help with Query/VBA to Sum the records in table1 in my database

I want to sum all if field_CC = "1002".

Thanks in advance.
Pedie
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
sum or count ?

Code:
select 
  count(*) as this_is_the_number_of_rows
from 
  my_table
where
(
  field_CC ='1002'
)

Code:
select 
  sum(field_1) as this_is_field_1_from_every_row_added_together
from 
  my_table
where
(
  field_CC ='1002'
)
 
Upvote 0
I tried this way....changing the field names and it errors

From SQL View
Code:
[/FONT]
[FONT=Courier New]SELECT Sum(Exps) AS Expr1
FROM Table1
WHERE (((Table1.[CC_N])="1009"));



Also tried this

Code:
[/FONT]
[FONT=Courier New]Dim LTotal As Currency
'LTotal = DSum("Exps", "Table1", "CC_N = 1009")
LTotal = DSum("Exps", "Table1", "CC_N = 1009", "Q1234 = QQ102")

I want to check 2 conditions if posible in dsum/sum whichever ways is possible..

sum or count ?

Rich (BB code):
Rich (BB code):
select 
  count(*) as this_is_the_number_of_rows
from 
  my_table
where
(
  field_CC ='1002'
)


Rich (BB code):
Rich (BB code):
select 
  sum(field_1) as this_is_field_1_from_every_row_added_together
from 
  my_table
where
(
  field_CC ='1002'
)

 
Upvote 0
Code:
SELECT Sum(Exps) AS Expr1
FROM Table1
WHERE (((Table1.[CC_N])='1009'));

The above *should* work - assumptions are that there are fields EXPS and CC_N in a table called TABLE1.

But you must be sure that CC_N is a TEXT field if you are putting the value in quotes. If CC_N is a NUMBER field then don't use the quotes around 1009. Also, be sure that EXPS has no NULL values in it as NULL cannot be summed and this ruins the total.


----------------------------------------------------------------------------

LTotal = DSum("Exps", "Table1", "CC_N = 1009", "Q1234 = QQ102")

The above looks odd. Is Q1234 a field?
 
Upvote 0
Xen, thank you very much!!!:) this is what I am looking for....

Thanks and Regards
Pedie
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,258
Members
452,901
Latest member
LisaGo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top