Solved

total sum of two columns

Posted on 2004-08-01
15
6,749 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

617 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