YTD Subquery problems

E

equalizer

I am trying to create a report that allows the user to select a
specific month and year for which to see MTD and YTD sales figures for
specific products. It must be a summary report so I only want it to
show product sales information for the selected month. Here's an
example:

Product Date MTD YTD
Product1 Feb, 2006 100 200
Prdouct2 Feb, 2006 200 250
Product3 Feb, 2006 300 350


After researching, I thought a subquery would be the best way to
accomplish this but I've never used subqueries and after several
attempts I have failed to get it to work the way I need it to. Can
anyone help me?
 
E

equalizer

Well, I've set up a form, named "Date Parameters," that asks the user
to select a month from a list, named "Month," and then to select a year
from a list as well, named "Year." This form is tied to a query with
some simple VBA code. The query will then use these inputs as values to
determine what month and year to show, then the report is also
popluated and opened. The end result of the query will show Product
Name, Month, Year, and Sales from a Transaction table. The month and
year values are determined using the form inputs in the criteria for
the OrderDate field (can be text or date format). I'm also bringing in
budget data from a Budget table but that's rather simple to do. The
real problem lies within the YTD information I need to retrieve. I need
to show YTD sales figures for each product in a summary report. All the
information I need for it is in that one Transaction table but there
are several transactions each day for each product so I guess I need to
manipulate that table in some way to be able to view YTD data. Any
ideas?
 
E

equalizer

Alright, I tried it but the RptDate fields are all blank and the MTD
and YTD fields are all zero. I guess I don't understand why MTD and YTD
expressions multiply the absolute value of the dates by the sales
amounts. Some sales amounts will be zero because we make Sales Orders
for things we give out to customers for free. Anyhow, it seems like
this is close.


Duane said:
Try something like:
Add a text box to the form Date Parameters
Name: txtRptDate
Control Source: DateValue([Year],[Month],1)
Visible: No

SELECT [Product], Forms![Date Parameters]!txtRptDate as RptDate,
Sum(Abs(Format(Orderdate,"yyyymm") = Format(Forms![Date
Parameters]!txtRptDate,"yyyymm"))*[Sales]) as MTD,
Sum(Abs(Format(Orderdate,"yyyy") = Format(Forms![Date
Parameters]!txtRptDate,"yyyy"))*[Sales]) as YTD
FROM [that one Transaction table]
GROUP BY [Product];
 
E

equalizer

Okay, I got the date to work correctly. To fix it I used DateSerial
instead of DateValue. I think DateValue was the wrong function to use
for it. Anyhow, unfortunately the problem isn't completely resolved
just yet because the YTD calulates the total year-to-date amount up to
the current date and not up to the date the the user specifies. For
example, if I choose February, 2006 from my form I want to see the
total amount up to February in my YTD (i.e. January total plus February
total) although it may be January of 2007. Right now the YTD shows the
same amount (YTD for the whole year) for any month in 2006 that I
choose. Is there a way to give YTD only up to the month that is chosen?

Thanks for your help!


Duane said:
Unhide the text box and check to make sure the correct date is displaying.
Reply with your actual SQL view that includes your exact SQL with your
control, field, form, and table names.

If your field data types aren't what we would expect, please let us know.

--
Duane Hookom
Microsoft Access MVP


equalizer said:
Alright, I tried it but the RptDate fields are all blank and the MTD
and YTD fields are all zero. I guess I don't understand why MTD and YTD
expressions multiply the absolute value of the dates by the sales
amounts. Some sales amounts will be zero because we make Sales Orders
for things we give out to customers for free. Anyhow, it seems like
this is close.


Duane said:
Try something like:
Add a text box to the form Date Parameters
Name: txtRptDate
Control Source: DateValue([Year],[Month],1)
Visible: No

SELECT [Product], Forms![Date Parameters]!txtRptDate as RptDate,
Sum(Abs(Format(Orderdate,"yyyymm") = Format(Forms![Date
Parameters]!txtRptDate,"yyyymm"))*[Sales]) as MTD,
Sum(Abs(Format(Orderdate,"yyyy") = Format(Forms![Date
Parameters]!txtRptDate,"yyyy"))*[Sales]) as YTD
FROM [that one Transaction table]
GROUP BY [Product];
--
Duane Hookom
Microsoft Access MVP


:

Well, I've set up a form, named "Date Parameters," that asks the user
to select a month from a list, named "Month," and then to select a year
from a list as well, named "Year." This form is tied to a query with
some simple VBA code. The query will then use these inputs as values to
determine what month and year to show, then the report is also
popluated and opened. The end result of the query will show Product
Name, Month, Year, and Sales from a Transaction table. The month and
year values are determined using the form inputs in the criteria for
the OrderDate field (can be text or date format). I'm also bringing in
budget data from a Budget table but that's rather simple to do. The
real problem lies within the YTD information I need to retrieve. I need
to show YTD sales figures for each product in a summary report. All the
information I need for it is in that one Transaction table but there
are several transactions each day for each product so I guess I need to
manipulate that table in some way to be able to view YTD data. Any
ideas?

Duane Hookom wrote:
Could you share how/where this happens "the user to select a specific month
and year"? Please provide form and control names for this.

Then provide your table/query fields and possibly data types if they aren't
obvious.
 
D

Duane Hookom

My bad on the DateValue vs DateSerial. You can try:

SELECT [Product], Forms![Date Parameters]!txtRptDate as RptDate,
Sum(Abs(Format(Orderdate,"yyyymm") = Format(Forms![Date
Parameters]!txtRptDate,"yyyymm"))*[Sales]) as MTD,
Sum(Abs(Year(OrderDate)=Year(Forms![Date Parameters]!txtRptDate) AND
Month(Orderdate) <= Month(Forms![Date Parameters]!txtRptDate))*[Sales]) as
YTD
FROM [that one Transaction table]
GROUP BY [Product];
--
Duane Hookom
Microsoft Access MVP


equalizer said:
Okay, I got the date to work correctly. To fix it I used DateSerial
instead of DateValue. I think DateValue was the wrong function to use
for it. Anyhow, unfortunately the problem isn't completely resolved
just yet because the YTD calulates the total year-to-date amount up to
the current date and not up to the date the the user specifies. For
example, if I choose February, 2006 from my form I want to see the
total amount up to February in my YTD (i.e. January total plus February
total) although it may be January of 2007. Right now the YTD shows the
same amount (YTD for the whole year) for any month in 2006 that I
choose. Is there a way to give YTD only up to the month that is chosen?

Thanks for your help!


Duane said:
Unhide the text box and check to make sure the correct date is displaying.
Reply with your actual SQL view that includes your exact SQL with your
control, field, form, and table names.

If your field data types aren't what we would expect, please let us know.

--
Duane Hookom
Microsoft Access MVP


equalizer said:
Alright, I tried it but the RptDate fields are all blank and the MTD
and YTD fields are all zero. I guess I don't understand why MTD and YTD
expressions multiply the absolute value of the dates by the sales
amounts. Some sales amounts will be zero because we make Sales Orders
for things we give out to customers for free. Anyhow, it seems like
this is close.


Duane Hookom wrote:
Try something like:
Add a text box to the form Date Parameters
Name: txtRptDate
Control Source: DateValue([Year],[Month],1)
Visible: No

SELECT [Product], Forms![Date Parameters]!txtRptDate as RptDate,
Sum(Abs(Format(Orderdate,"yyyymm") = Format(Forms![Date
Parameters]!txtRptDate,"yyyymm"))*[Sales]) as MTD,
Sum(Abs(Format(Orderdate,"yyyy") = Format(Forms![Date
Parameters]!txtRptDate,"yyyy"))*[Sales]) as YTD
FROM [that one Transaction table]
GROUP BY [Product];
--
Duane Hookom
Microsoft Access MVP


:

Well, I've set up a form, named "Date Parameters," that asks the user
to select a month from a list, named "Month," and then to select a year
from a list as well, named "Year." This form is tied to a query with
some simple VBA code. The query will then use these inputs as values to
determine what month and year to show, then the report is also
popluated and opened. The end result of the query will show Product
Name, Month, Year, and Sales from a Transaction table. The month and
year values are determined using the form inputs in the criteria for
the OrderDate field (can be text or date format). I'm also bringing in
budget data from a Budget table but that's rather simple to do. The
real problem lies within the YTD information I need to retrieve. I need
to show YTD sales figures for each product in a summary report. All the
information I need for it is in that one Transaction table but there
are several transactions each day for each product so I guess I need to
manipulate that table in some way to be able to view YTD data. Any
ideas?

Duane Hookom wrote:
Could you share how/where this happens "the user to select a specific month
and year"? Please provide form and control names for this.

Then provide your table/query fields and possibly data types if they aren't
obvious.
 
E

equalizer

Beautiful! It works! Thank you so much. You saved me a lot of headache!

Duane said:
My bad on the DateValue vs DateSerial. You can try:

SELECT [Product], Forms![Date Parameters]!txtRptDate as RptDate,
Sum(Abs(Format(Orderdate,"yyyymm") = Format(Forms![Date
Parameters]!txtRptDate,"yyyymm"))*[Sales]) as MTD,
Sum(Abs(Year(OrderDate)=Year(Forms![Date Parameters]!txtRptDate) AND
Month(Orderdate) <= Month(Forms![Date Parameters]!txtRptDate))*[Sales]) as
YTD
FROM [that one Transaction table]
GROUP BY [Product];
--
Duane Hookom
Microsoft Access MVP


equalizer said:
Okay, I got the date to work correctly. To fix it I used DateSerial
instead of DateValue. I think DateValue was the wrong function to use
for it. Anyhow, unfortunately the problem isn't completely resolved
just yet because the YTD calulates the total year-to-date amount up to
the current date and not up to the date the the user specifies. For
example, if I choose February, 2006 from my form I want to see the
total amount up to February in my YTD (i.e. January total plus February
total) although it may be January of 2007. Right now the YTD shows the
same amount (YTD for the whole year) for any month in 2006 that I
choose. Is there a way to give YTD only up to the month that is chosen?

Thanks for your help!


Duane said:
Unhide the text box and check to make sure the correct date is displaying.
Reply with your actual SQL view that includes your exact SQL with your
control, field, form, and table names.

If your field data types aren't what we would expect, please let us know.

--
Duane Hookom
Microsoft Access MVP


:

Alright, I tried it but the RptDate fields are all blank and the MTD
and YTD fields are all zero. I guess I don't understand why MTD and YTD
expressions multiply the absolute value of the dates by the sales
amounts. Some sales amounts will be zero because we make Sales Orders
for things we give out to customers for free. Anyhow, it seems like
this is close.


Duane Hookom wrote:
Try something like:
Add a text box to the form Date Parameters
Name: txtRptDate
Control Source: DateValue([Year],[Month],1)
Visible: No

SELECT [Product], Forms![Date Parameters]!txtRptDate as RptDate,
Sum(Abs(Format(Orderdate,"yyyymm") = Format(Forms![Date
Parameters]!txtRptDate,"yyyymm"))*[Sales]) as MTD,
Sum(Abs(Format(Orderdate,"yyyy") = Format(Forms![Date
Parameters]!txtRptDate,"yyyy"))*[Sales]) as YTD
FROM [that one Transaction table]
GROUP BY [Product];
--
Duane Hookom
Microsoft Access MVP


:

Well, I've set up a form, named "Date Parameters," that asks the user
to select a month from a list, named "Month," and then to select a year
from a list as well, named "Year." This form is tied to a query with
some simple VBA code. The query will then use these inputs as values to
determine what month and year to show, then the report is also
popluated and opened. The end result of the query will show Product
Name, Month, Year, and Sales from a Transaction table. The month and
year values are determined using the form inputs in the criteria for
the OrderDate field (can be text or date format). I'm also bringing in
budget data from a Budget table but that's rather simple to do. The
real problem lies within the YTD information I need to retrieve. I need
to show YTD sales figures for each product in a summary report. All the
information I need for it is in that one Transaction table but there
are several transactions each day for each product so I guess I need to
manipulate that table in some way to be able to view YTD data. Any
ideas?

Duane Hookom wrote:
Could you share how/where this happens "the user to select a specific month
and year"? Please provide form and control names for this.

Then provide your table/query fields and possibly data types if they aren't
obvious.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top