Access Macro with changing date range

kmacd6951

Board Regular
Joined
Mar 3, 2004
Messages
58
New to Access - hate that you can't do Macro's like you can in Excel (record key strokes)... anyway.

Is there a way to create a macro that would 1).open my query, 2).enter the date range (I get two pop up screens for FROM and THRU date currently). The date range would need to be Yesterday's date thru Today's date. 3). Give me the output in Pivot Table view.

Does someone have an example of this or something similar that I could work from?

Thanks!
Katie
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Actually, you can do this all from a single Crosstab query. And, the query can automatically figure out 'yesterday' without the use of any code/macros.

Here's a quick sample produced by QBE (query by example wizard interface)
It makes the assumption that you ALWAYS use yesterday as the start date and that nothing is future dated. If anything can be future dated, just expand the 'WHERE' condition to specify a maximum value.

This uses the built in 'Date' function that is equal to today's system date.

Code:
TRANSFORM Count(tblData.File) AS CountOfFile
SELECT tblData.Commodity, Count(tblData.File) AS [Total Of File]
FROM tblData
WHERE (((tblData.Datefld)>=Date()-1))
GROUP BY tblData.Commodity
PIVOT tblData.C;

As an alternative, you can borrow something like this that figures out the prior workday (copy into a module) - this is code.

http://www.mvps.org/access/datetime/date0012.htm

And then change your query to

Code:
TRANSFORM Count(tblData.File) AS CountOfFile
SELECT tblData.Commodity, Count(tblData.File) AS [Total Of File]
FROM tblData
WHERE (((tblData.Datefld)>=dhPreviousWorkdayA(Date())))
GROUP BY tblData.Commodity, tblData.Date
PIVOT tblData.C;

This doesn't allow you to type in a date, but would cover you running this on Monday to reflect Friday Data, if that is what you need. If you'd like to type in dates, you'll end up using InputBoxes which are used to 'build' a query which you save as a QueryDef

Mike
 
Upvote 0
So, does this mean I should recreate my current query as a crosstab query instead? or do I add in the TRANSFORM line to the top of the SQL of my current query?

Thank you!
 
Upvote 0
So... what if I want to set up the Macro to run for 7 days back through today?

I tried to use the Action of ApplyFilter and my Where Condition says:

Between [Cases Opened Query]![SWDATECREATED] (Date () -7) And [Cases Opened Query]![SWDATECREATED] Date ()

But it tells me this is invalid. Actually it says that MS Access can't Parse the expression
Does anyone know what I am missing??
 
Upvote 0
This should work. Between should be unnecessary unless you're attempting to do something like pick a 7 day time period from a month ago.

Code:
WHERE (((tblData.Datefld)>=Date()-7))

You're in one of my experience 'grey' areas. I know what works for me, but never worried about the precise reason why it doesn't work.

What I've noticed is sometimes, when building complex expressions within a Crosstab (TRANSFORM) query, Access can not handle it. I think this is due to how SQL was implemented within Access (kinda half-azzed sometimes)

A workaround is to do this in two steps. Build a select query that does your manipulations and then base the Crosstab query off it. If that doesn't work, I tend to start fiddling until I get something to work.

Mike
 
Upvote 0

Forum statistics

Threads
1,221,645
Messages
6,161,044
Members
451,682
Latest member
ogoreo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top