Solved

total sum of two columns

Posted on 2004-08-01
15
6,720 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
 
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 119

Expert Comment

by:Rey Obrero
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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 119

Accepted Solution

by:
Rey Obrero 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now