Solved

total sum of two columns

Posted on 2004-08-01
15
6,731 Views
Last Modified: 2008-01-09
I have two columns that I need to total together as a value for one field. NB_CON and COI_CON are the two columns. Any ideas? Thanks


=DSum("NB_CON","COI_CON","tblActivity","Date_Added Between DateSerial(Year(Date()),Month(Date()),1) And DateSerial(Year(Date()),Month(Date())+1,0)")
0
Comment
Question by:robsking
  • 7
  • 4
  • 2
  • +2
15 Comments
 
LVL 2

Expert Comment

by:kaaviyam
ID: 11687081
where do you want the total to be stored, ie is the sum to be stored in field in the same table?
0
 

Author Comment

by:robsking
ID: 11687086
I want the total to be stored in a column named Total_NB in the same table. Thanks
0
 

Author Comment

by:robsking
ID: 11687087
I meant field named Total_NB. Thanks
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 15

Expert Comment

by:will_scarlet7
ID: 11687422
Would you want to run an update query to set field Total_NB to NB_CON + COI_CON?
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 11687756
perform two dsums, but then that would be slow otherwise create your own recordset

dim rs as DAO.Recordset
set rs=currentdb.Openrecordset("SELECT SUM(NB_CON)+SUM(COI_CON) AS MyTotal FROM tblActivity WHERE DateAdded Between #" & DateSerial(Year(Date()),Month(Date()),1) & "# AND #" & DateSerial(Year(Date()),Month(Date())+1,0) & "#")
if rs.BOF = True or rs.EOF = True then
     dMyTotal = 0
else
     dMyTotal = rs!MyTotal
end if
rs.Close
set rs=nothing




Not sure about the #, thats what u normally use for date comparison, I tend to use format and single quotes but give that a try
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 11688025
<I want the total to be stored in a column named Total_NB in the same table.>

Normally you don't want to store calculated values in a table.

you can use the query to find the row sum of NB_CON and COI_CON

        Select Nz([NB_CON],0)+Nz([COI_CON],0) AS Total_NB FROM tblActivity;

If you want to store it to your table use an update query
     
        Update tblActivity Set Total_NB=Nz([NB_CON],0)+Nz([COI_CON],0)

if you want to display the total of  total_NB on a textbox in a form based on the criteria you posted above

Private Sub Command0_Click()
Dim rs  As Recordset, sSQL As String
sSQL = "SELECT Sum((Nz([NB_CON],0)+Nz([COI_CON],0))) AS Total_NB "
sSQL = sSQL & "FROM tblActivity "
sSQL = sSQL & "WHERE (((tblActivity.Date_Added) "
sSQL = sSQL & "Between DateSerial(Year(Date()),Month(Date()),1) "
sSQL = sSQL & "And DateSerial(Year(Date()),Month(Date())+1,0)));"

Set rs = CurrentDb.OpenRecordset(sSQL)

    Me.txtTotal_NB = Nz(rs("Total_NB"), 0)
rs.Close
Set rs = Nothing

End Sub


0
 

Author Comment

by:robsking
ID: 11688089
I was hoping to put it in the control source of a textbox. Any ideas?
0
 

Author Comment

by:robsking
ID: 11688187
The problem is that I don't have a column in my table named Total_NB. Just a textbox named Total_NB. I was hoping to be able to refesh the value in the textbox each time the form opens. It would count the total of records in the NB_CON column and the total of records in the COI_CON column for this particluar month, then give me the combined total in a text box named Total_NB.
The code below works fine in the control source to give me the total records in column NB_CON for the current month. Can we modify the code below? Thanks

=DSum("NB_CON","tblActivity","Date_Added Between DateSerial(Year(Date()),Month(Date()),1) And DateSerial(Year(Date()),Month(Date())+1,0)")
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 11688247
like I said before, perhaps u could do two DSUM's but then it will be a little slow

thats why I suggested code, on the Form_Current, u cant set Total_NB to be the value of rs!MyTotal
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 11688260
I don't know exacly what your form is intended for but, you can try this
place the codes in the form load event, or any other event that you think the textfield need to be updated

Private Sub Form_Load()
Dim rs  As Recordset, sSQL As String
sSQL = "SELECT Sum((Nz([NB_CON],0)+Nz([COI_CON],0))) AS Total_NB "
sSQL = sSQL & "FROM tblActivity "
sSQL = sSQL & "WHERE (((tblActivity.Date_Added) "
sSQL = sSQL & "Between DateSerial(Year(Date()),Month(Date()),1) "
sSQL = sSQL & "And DateSerial(Year(Date()),Month(Date())+1,0)));"

Set rs = CurrentDb.OpenRecordset(sSQL)

    Me.txtTotal_NB = Nz(rs("Total_NB"), 0)
rs.Close
Set rs = Nothing

End Sub
0
 

Author Comment

by:robsking
ID: 11688346
Thanks Capricorn! I understand, now.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 11688712
good stuff robsking, now that u understand Capricorn1's solution, do u understand mine
though it may not be perfect, I was more talking about how to use recordsets and getting the sum from there
0
 

Author Comment

by:robsking
ID: 11688969
rockiroads,
I found capricorn's code more readable and with a pretty fair explanation how to apply it.
 I thank you for your contribution, as well.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 11689001
ok, thanks, thats good feedback, helps me to write my solutions a bit better
0
 

Author Comment

by:robsking
ID: 11689079
rockiroads,
I kind of consider all the extra explanations given by capricorn1, code for dummies. The how and why really does help.
Anyway, I'll be posting another complicated question later.
Thanks to everyone taking the time for the beginners!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question