473,503 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sum Query

266 Contributor
Hi all,

My goal is to run a report for every month. The information i want in the report is the sum of a field. Actually 6 different fields. I have a query with the following code

Expand|Select|Wrap|Line Numbers
  1. SELECT Sum(IIf([master.REPAIR],1,0)) AS SumOfREPAIR, Sum(IIf([master.REPLACE],1,0)) AS SumOfREPLACE, Sum(IIf([master.remove],1,0)) AS SumOfREMOVE, Sum(IIf([master.install],1,0)) AS SumOfINSTALL, Sum(IIf([master.maintenance],1,0)) AS SumOfMAINTENANCE, Sum(IIf([master.TEMPDISCONNECT],1,0)) AS SumOfTEMPDISCONNECT, Master.JOBTYPE
  2. FROM Master
  3. WHERE (((Master.COMPLETEDINFIELD) Is Not Null And (Master.COMPLETEDINFIELD) Like [Please Enter Month ex- 9/*/*]))
  4. GROUP BY Master.JOBTYPE;
This works great when i run the query, but i need to report on these numbers using a specific date range. Since the completedinfield, field is using a where clause it will not show and therefore cannot use it in my report. Is there a better way to do this? Thanks for the help.

I have attached a sample of my report.
Attached Files
File Type: pdf Report.pdf (9.8 KB, 193 views)
Jul 18 '14 #1
34 1598
Seth Schrock
2,965 Recognized Expert Specialist
What I would do is create a form that has two textboxes in which you can enter a starting and ending date. Then have your query reference the form for your criteria. If you need the date range on your report, you can pass the values to the report using the report's OpenArgs property or just reference the form in your report's OnLoad event and assign the values to a control in your header or something like that.
Jul 18 '14 #2
didacticone
266 Contributor
I think in concept i understand what your saying. Should the textboxes be linked to fields in the underlying table? And by doing this it would filter my report based on that date range? Could you possibly give me an example? I appreciate your help.
Jul 18 '14 #3
Seth Schrock
2,965 Recognized Expert Specialist
Neither the form nor the two textboxes would be bound. For ease of illustration, I'll name my form frmSearch and my two textboxes txtStart and txtEnd. Your query would then be
Expand|Select|Wrap|Line Numbers
  1. SELECT Sum(IIf([master.REPAIR],1,0)) AS SumOfREPAIR
  2. , Sum(IIf([master.REPLACE],1,0)) AS SumOfREPLACE
  3. , Sum(IIf([master.remove],1,0)) AS SumOfREMOVE
  4. , Sum(IIf([master.install],1,0)) AS SumOfINSTALL
  5. , Sum(IIf([master.maintenance],1,0)) AS SumOfMAINTENANCE
  6. , Sum(IIf([master.TEMPDISCONNECT],1,0)) AS SumOfTEMPDISCONNECT
  7. , Master.JOBTYPE
  8. FROM Master
  9. WHERE COMPLETEDINFIELD Is Not Null 
  10. And COMPLETEDINFIELD Between Forms!frmSearch!txtStart and Forms!frmSearch!txtEnd
  11. GROUP BY Master.JOBTYPE;
As far as getting the date range into the report, I would recommend the OpenArgs method. Just put a button on your form (frmSearch as I named it) that runs the report using the DoCmd.OpenReport method and add the text you want in the OpenArgs argument. Then, in your report's On_Load event, you can assign this value to a textbox or a label's caption.
Expand|Select|Wrap|Line Numbers
  1. Me.txtHeader = Me.OpenArgs
Jul 18 '14 #4
didacticone
266 Contributor
I have created the form and the button to open the report as you said. When i click the button the report displays a box the says "completedinfield" with a text box under it and i can hit ok or cancel. It does seem as though it is querying the date range properly. Is there a reason this box is coming up. Have i dont something wrong?
Jul 18 '14 #5
Seth Schrock
2,965 Recognized Expert Specialist
Make sure that the field name is typed correctly. Basically, when a query doesn't recognise a field, it asks for its value if it hasn't already been set in the query.
Jul 18 '14 #6
didacticone
266 Contributor
my understanding is that when using a where clause it cannot be displayed in the report? is this correct and why this is possibly happening?
Jul 18 '14 #7
Seth Schrock
2,965 Recognized Expert Specialist
The fields do not display. They are only criteria. However, if the same field is included in the SELECT portion of the query, then it does display. However, since you are using an aggregate query (Sum function), you would also have to include it in the GROUP BY clause which would group by the date. I think that you could group it based on the month, but this still doesn't allow you filter your results inside the report.
Jul 18 '14 #8
didacticone
266 Contributor
That was my initial conundrum. I want that field to be included but as you said i would have to include it in the group by which is something i do not want to do. any ideas?
Jul 18 '14 #9
Seth Schrock
2,965 Recognized Expert Specialist
That is why I think that it would be easier to filter your results in the WHERE clause of the query. The other option would be to have the controls in your report do the summing of the data. Personally, I don't prefer this method as it seems to take longer to do that sort of thing in a report than in a query. I like to have my data just as I want it when it gets to the report instead of working with it after.

Can you post what you tried when you got the popup in post #5?
Jul 18 '14 #10
didacticone
266 Contributor
I did exactly what you said in post 4. I took your code and created a new query based on that. then created the new form with the text boxes. And everything is working. the dates seem to be filtering fine. i did the open arg method to get my text into the report. but that box just pops up because "completedinfield" is not in the report.
Jul 18 '14 #11
Seth Schrock
2,965 Recognized Expert Specialist
Does your report reference the completedinfield field? Can you post your SQL code? It is more likely that the popup is from the query and not the report. However, since the report is based on the query, running the report will prompt the query to show the popup even though you didn't specifically open the query.
Jul 18 '14 #12
didacticone
266 Contributor
Expand|Select|Wrap|Line Numbers
  1. SELECT Sum(IIf([master.REPAIR],1,0)) AS SumOfREPAIR, Sum(IIf([master.REPLACE],1,0)) AS SumOfREPLACE, Sum(IIf([master.remove],1,0)) AS SumOfREMOVE, Sum(IIf([master.install],1,0)) AS SumOfINSTALL, Sum(IIf([master.maintenance],1,0)) AS SumOfMAINTENANCE, Sum(IIf([master.TEMPDISCONNECT],1,0)) AS SumOfTEMPDISCONNECT, Master.JOBTYPE
  2. FROM Master
  3. WHERE COMPLETEDINFIELD Is Not Null And COMPLETEDINFIELD Between Forms!frmSearch!txtStart And Forms!frmSearch!txtEnd
  4. GROUP BY Master.JOBTYPE;
  5.  
Jul 18 '14 #13
Seth Schrock
2,965 Recognized Expert Specialist
Try making your WHERE clause be
Expand|Select|Wrap|Line Numbers
  1. WHERE COMPLETEDINFIELD Between Forms!frmSearch!txtStart And Forms!frmSearch!txtEnd
Jul 18 '14 #14
didacticone
266 Contributor
same thing...unfortunately
Jul 18 '14 #15
Seth Schrock
2,965 Recognized Expert Specialist
Try
Expand|Select|Wrap|Line Numbers
  1. WHERE (Master.COMPLETEDINFIELD) Between Forms!frmSearch!txtStart And Forms!frmSearch!txtEnd 
Jul 18 '14 #16
didacticone
266 Contributor
still no luck my friend
Jul 18 '14 #17
Seth Schrock
2,965 Recognized Expert Specialist
Okay, we are going to have to try something to get that field to be recognized. First remove your WHERE clause and then open the query in Design view (where you see the table(s) that the query is based on). Now, find the COMPLETEDINFIELD field in the table and double click on it. This should add it to the list. Uncheck the Show checkbox. Now in the criteria field, enter Between Forms!frmSearch!txtStart And Forms!frmSearch!txtEnd . See if that does it.
Jul 18 '14 #18
didacticone
266 Contributor
still coming up and now the report isnt grouped by job type its grouped by the date
Jul 18 '14 #19
Seth Schrock
2,965 Recognized Expert Specialist
It is grouping by the date now because it has been added to the GROUP BY clause. One more thing that I have thought of. In the query properties, there is a property called parameters. Make sure that there isn't anything in there.

If that doesn't work, I'm stumped as to why that popup keeps appearing. You would probably have to then make your query not an aggregate query and then do your summing and filtering in the report.
Jul 20 '14 #20
didacticone
266 Contributor
I am glad it is not just me who is baffled by this... if i was to do it in the report can you give me some help on that? i have never done and filtering in a report itself. thank you for your help
Jul 21 '14 #21
Seth Schrock
2,965 Recognized Expert Specialist
You would just make the control source of each control in the report have the sum function in it. Like this
Expand|Select|Wrap|Line Numbers
  1. =Sum(IIf([master.REPAIR],1,0))
You would then set your report's filter property using VBA, probably in the On_Open event.
Expand|Select|Wrap|Line Numbers
  1. Me.Filter = Me.OpenArgs
You just need to setup your filter string when you open the report that you can pass it the correct value which is the same as your WHERE clause without the word WHERE.
Jul 21 '14 #22
didacticone
266 Contributor
i changed the control sources for all the fields. just confused as to how to put the where clause.
Jul 21 '14 #23
Seth Schrock
2,965 Recognized Expert Specialist
You would remove the WHERE clause from the query.
Jul 21 '14 #24
didacticone
266 Contributor
my query is now as follows:

Expand|Select|Wrap|Line Numbers
  1. SELECT Master.JOBTYPE, Master.COMPLETEDINFIELD, Master.REPAIR, Master.REPLACE, Master.REMOVE, Master.INSTALL, Master.MAINTENANCE, Master.TEMPDISCONNECT
  2. FROM Master;
  3.  
Jul 21 '14 #25
Seth Schrock
2,965 Recognized Expert Specialist
That is correct. When you run it, do you get any popups?
Jul 21 '14 #26
didacticone
266 Contributor
no pop ups, but the job type is not grouped in the report, and i am not sure how to query the dates i want.
Jul 21 '14 #27
paulwilliam024
1 New Member
There are some intrusting and i like this..
Jul 21 '14 #28
Seth Schrock
2,965 Recognized Expert Specialist
You can add groupings in the report design view. In Access 2010, it is in the Design tab, Grouping & Totals section (toward the left side).

To get the dates that you want I'm now thinking that you could use the WhereCondition argument when opening the report instead of the OpenArgs.
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenReport ReportName:="yourReport", WhereCondition:="COMPLETEDINFIELD BETWEEN #" & Me.txtStart & "# And #" & Me.txtEnd & "#"
Jul 21 '14 #29
didacticone
266 Contributor
Im having all sorts of problems here, i was able to group by job type, but the summation is all wrong. it is just adding and showing the same number for every job type. so for instance if there are 3 total repair jobs, 1 for 3 different categories, it is showing 3 in every category? again im sorry ive never worked with reports like this before.
Jul 21 '14 #30
didacticone
266 Contributor
i actually fixed the grouping issue. now it is displaying the numbers correctly. any idea on the best way to handle inputting specific dates when i run the report?
Jul 21 '14 #31
Seth Schrock
2,965 Recognized Expert Specialist
What do you mean by inputting? Are you adding records?
Jul 21 '14 #32
didacticone
266 Contributor
sorry, meant querying. it was actually just an oversight on my part to refer it to the form for entering dates that you had advised me to make earlier. seems to be working very well now. i think i just have one last question. if there is no value entered for a job type, so for instance valves, has 0 of (repair, replace, etc.) it is not displaying on the report. is there a way to have this display, just showing "0"'s. thank you so much for your help.
Jul 21 '14 #33
Seth Schrock
2,965 Recognized Expert Specialist
As this is a separate question and one that can't be answered very quickly, you will need to post this in a new thread.
Jul 21 '14 #34
didacticone
266 Contributor
ok. thank you for your help. i truly appreciate it.
Jul 21 '14 #35

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3413
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
29
2477
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
9
3108
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
3
5372
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
14
3853
by: Dave Thomas | last post by:
If I have a table set up like this: Name | VARCHAR Email | VARCHAR Age | TINYINT | NULL (Default: NULL) And I want the user to enter his or her name, email, and age - but AGE is optional. ...
0
3484
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
15
4385
by: Richard Hollenbeck | last post by:
I tried to ask this question before on the 14th of January but I never got a reply. I'm still struggling with the problem. I'll try to rephrase the question: I have a crosstab query with rows...
6
4813
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
3
1947
by: cover | last post by:
I have a table with 50 fields that receive input depending on whether that input came in from a 'shaker' form or a 'conveyor' form. Input from the 'conveyor' form might populate 25 fields while...
1
2030
by: accessonion | last post by:
Hi, I have finally been converted to Access as the data set I’m using grew to a ridiculous size. I am analysing one years’ worth of data and the database itself has grown to be quite large at just...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.