Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

total sum of two columns

Posted on 2004-08-01
15
Medium Priority
?
6,761 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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 2000 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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

971 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