Solved

total sum of two columns

Posted on 2004-08-01
15
6,710 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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Familiarize people with the process of utilizing SQL Server views 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 Access…
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 …

707 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

21 Experts available now in Live!

Get 1:1 Help Now