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

x
?
Solved

DSum Multiple Criteria

Posted on 2013-06-07
13
Medium Priority
?
512 Views
Last Modified: 2013-06-07
I'm trying to create a DSum that uses 2 different criterias. I have one already set up and it works fine but I need to add another that references a control on my main form, not the subform where the DSum is. I tried just referencing the control on the form in the query criteria but I get the NAME# error. So I'm guessing I need to add this to the criteria of the DSum formula. Here's the code that I have so far without the second criteria:

DSum("fqtyrecv","qryPOHistory","Format([fdaterecv],'mmm-yy')='" & Format([Forms]![sfrmPurchaseAnalysisPOHisto

Open in new window


I need to add the criteria that uses the "fpartno" in the qryPOHistory and it needs to equal the cbofpartno on my frmPurchasingAnalysis1 form. How can I add that to the DSum formula? And why if I just put this "[Forms]![frmPurchasingAnalysis1]![cbofpartno]" in the criteria of the query itself do I get the NAME# error?
0
Comment
Question by:Lawrence Salvucci
  • 6
  • 6
13 Comments
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 39228975
I cannot read the full code line.

/gustav
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 39228981
=DSum("fqtyrecv","qryPOHistory",
"Format([fdaterecv],'mmm-yy')='" & 
Format([Forms]![sfrmPurchaseAnalysisPOHistory]![txtMonth1],'mmm-yy') & "'")

Open in new window

0
 
LVL 85
ID: 39229001
Your code segment is not complete, but if you want to add a new criteria, you'd do something like this:

DSum("fqtyrecv","qryPOHistory","Format([fdaterecv],'mmm-yy')='" & Format([Forms]![sfrmPurchaseAnalysisPOHistory].SomeControl & "' AND fPartNo='" & Forms!frmPurchasingAnalysis1.cbofPartNo & "'")

Note too that if you're dealing with Text values, you need to enclose them in single quotes or double quotes, as you've done with the fdaterecv value.
why if I just put this "[Forms]![frmPurchasingAnalysis1]![cbofpartno]" in the criteria of the query itself do I get the NAME# error?
That's hard to say without seeing your project, but in general the error means Access can't find that control. If you're using frmPurchasingAnalysis1 as a Subform, then you'd have to refer to it like this:

Forms!FormWithTheSubformCONTROL.NameOfTheSubformCONTROL.NameOfYourControl

Note "NameOfTheSubformCONTROL" - this may or may not be the same the form you're using as a Subform, so be sure to get that right.
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 52

Expert Comment

by:Gustav Brock
ID: 39229008
Then it could be:
=DSum("fqtyrecv","qryPOHistory",
"Format([fdaterecv],'mmm-yy')='" & 
Format([Forms]![sfrmPurchaseAnalysisPOHistory]![txtMonth1],'mmm-yy') & "' And fpartno = " & [Forms]![sfrmPurchaseAnalysisPOHistory].[Parent]![cbofpartno] & "")
 

Open in new window


Of course, if fpartno is a string, single quotes must be wrapped around the value as for the first expression.

/gustav
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 39229039
Still getting the #NAME? error message. The frmPurchaseAnalysis1 is the parent form and sfrmPurchasingAnalysisPOHistory is the subform where the DSum is. When the users select a part no from the cbofpartno then that's when it should fire the DSum on the subform but it's still not working. Here's what I have for the formula:


=DSum("fqtyrecv","qryPOHistory","Format([fdaterecv],'mmm-yy')='" & 
Format([Forms]![sfrmPurchaseAnalysisPOHistory]![txtMonth1],'mmm-yy') & 
"' And fpartno = " & [Forms]![sfrmPurchaseAnalysisPOHistory].[Parent]![cbofpartno] & "")

Open in new window

0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 39229083
How about this simplified:
=DSum("fqtyrecv","qryPOHistory","Format([fdaterecv],'mmm-yy')='" & 
Format([txtMonth1],'mmm-yy') & 
"' And fpartno = " & [Forms]![frmPurchaseAnalysis1]![cbofpartno] & "")

Open in new window

/gustav
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 39229151
Still not working. When I take out the second criteria it works fine. But once I add the fpartno criteria portion is when I still get the #NAME?. The fpartno is a text value if that makes any difference. I would post an example but my tables are linked to my SQL Database tables.
0
 
LVL 52

Accepted Solution

by:
Gustav Brock earned 2000 total points
ID: 39229224
If text value, single quotes must be applied:
=DSum("fqtyrecv","qryPOHistory","Format([fdaterecv],'mmm-yy')='" & 
Format([txtMonth1],'mmm-yy') & 
"' And fpartno = '" & [Forms]![frmPurchaseAnalysis1]![cbofpartno] & "'")

Open in new window


If you open the form and run this in the debug window, what do you see:

? [Forms]![frmPurchaseAnalysis1]![cbofpartno]

/gustav
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 39229241
When I run that in the debug window I get "510125", which is the value in the cbofpartno.

Now when I changed the formula to your latest version I'm getting a flashing #ERROR
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 39229245
Scratch that. I typed something wrong in your latest code. It's working now.
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 39229256
Great!

/gustav
0
 
LVL 1

Author Closing Comment

by:Lawrence Salvucci
ID: 39229263
Thanks for all your help! I greatly appreciate it!!
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 39229269
You are welcome!

/gustav
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
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.
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…

885 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