Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Is there a way to do the equivalent of Excel's Sumifs() in an Access Expression (or VBA)

Posted on 2010-11-26
9
Medium Priority
?
1,785 Views
Last Modified: 2012-05-10

I have a table in Access with information (see attached).  I'm trying to figure out if there is a way to get the sum of a column (field) where a certain criteria in two other fields is met.  For example, I'd like to get year to date (YTD) totals for a given account.  So, I'd need the total sales for a given account (criteria #1) for all months between Jan. and the current records Month.  Does anyone know if there is a way to do this in Access?
Sumifs-AccessEquivalent.xlsx
0
Comment
Question by:BBlu
[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
  • 5
  • 2
  • 2
9 Comments
 
LVL 44

Accepted Solution

by:
GRayL earned 800 total points
ID: 34220335
This do it?

SELECT Sum([Field] from myTable WHERE Year(AcctDate) = Year(Date()) AND AcctNo = [Enter Acct No];
0
 

Author Comment

by:BBlu
ID: 34220346
Probably pretty close, but I'd like they date criteria to be for any date on or before the date of the current record.  How would that change the code? Also, that is a SQL statement.  Is there an expression that I could use in a a query to add a field with that data?
0
 
LVL 12

Expert Comment

by:Missus Miss_Sellaneus
ID: 34220352
you need something like this..

SELECT acct, SUM(IIF(Month >= january AND Month <= currentmonth, Sales, 0)) AS YTDSales
 FROM table1
 HAVING YTDSales <> 0



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:BBlu
ID: 34220364
Thank you very much.  But that would be the sql for a query, right?  Is there a way to do it in an expression.
0
 
LVL 12

Assisted Solution

by:Missus Miss_Sellaneus
Missus Miss_Sellaneus earned 800 total points
ID: 34220377
Maybe I got a little too fixated on the IIF part of your question.

Do you want to see only those rows that had sales during the given date range? If so, you only need to use a WHERE clause like GRavL showed you.

If you want to include customer records that may not have sales in that date range, you'll need the SUM/IIF.

Do you want to prompt the user for the date range?
0
 

Author Comment

by:BBlu
ID: 34220392
I'm actually trying to reproduce an excel file our sales team has been manually doing for years.  We have all of the data in two access tables.  For each account we have a matrix with columns for all months in the current period.  We then have rows for Sales, YTD sales, Payments, YTD Payments.  (If I can figure out one, I can do both; that's why I didn't mention the pmts. part).  Anyhow, I was thinking I could mimic the excel file by:

1. creating a query and adding a field/column for the YTD total for each record.  For example, if the record has:
a. Account= 1234
b. Month=3/1/10 (March 2010)
c. Sales= $340
I could add a field
d. YTDSales = $640 (if Jan was $200 and Feb was $100, for example)

2. I could then do a Union Query something like
Select "Sales" as Category, Account, Month, Sales from SalesTable
Union All
Select "YTD" as Category, Account, Month, YTDSales from SalesQuery

And if I I had a table that ordered the 'Category' Field, I could reproduce what I'm going after (see attached)
DesiredReport.xlsx
0
 

Author Comment

by:BBlu
ID: 34220494
I almost figured it out..I guess the real question is:
Is there a way to have multiple criteria in DSum?

DSum("UnitPrice", "Order Details", "OrderID = 10248")
0
 

Author Comment

by:BBlu
ID: 34220542
I figured out that you can use "AND" to add multiple criteria.  But I did not know how to do it in SQL either, so you helped.  Thanks.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 34222919
Thanks, glad to help.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

730 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