Union Query Date pass through

CJ

Board Regular
Joined
Feb 22, 2002
Messages
77
Hi,

I have a union query that links 10 queries together and this code in the date field to alway run for last month
Between DateSerial(Year(Date()),Month(Date())-1,1) And DateSerial(Year(Date()),Month(Date()),0)

This works well...

I have being asked to copy the above and have it set up to run different month periods setups (muliple months) this means amending the code but then opening all 10 queries to amend the order date field.

My Question is there anyway I can just add the date code in the 1st query and have all the other 9 pick up that date code, it would save me so much time.

Thanks
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I've been using this lately, it probably will adapt, but I don't know how

declare @BEGINDATE datetime, @ENDDATE datetime
SET @BEGINDATE = '20151201'
Set @ENDDATE = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@BEGINDATE)+1,0))

The end date allows me to get the last day of the month set in @begindate

It saved me editing both every time I ran for a bit more information, and adjusts for 28, 30 and 31 days automatically
 
Upvote 0
I would suggest having your queries link to a date that is stored in a table or form record. Then you can use it in all the queries but update it "in one place". I like a form because that becomes my "tool" for running the queries - open the form, set the dates, use a button to kick off the query.

For example, using a form:
Code:
select * from Table1 
where TransDate >= (DateValue([Forms].[Form1].[StartDate])) AND TransDate <=(DateValue([Forms].Form1].[EndDate]))
IN this case, Form1 is the form and [StartDate] and [EndDate] are the fields on the form (textboxes).

One wrinkle is that textboxes can be free-form and I don't like text for dates. So my general practice is to have a table that the form is bound to, and the textboxes on the form are bound to two date fields in the table. In this case, you can then link to the form fields or the actual table (since they are bound, the value is the same). This ensures correct typing of data as valid date data types. Also since you have bona fide dates now, you don't need the date value function anymore.

One more wrinkle is you don't want that table to have multiple records. So I will give the table and ID field, with a numeric data type, with a validation rule that the value must be equal to 1, and make it the primary key. So there can never, ever be more than one record in this table. Potential disaster avoided.

So, in summary - have a location for you dates, in a table or form, so you can use it in your queries and update in in one place as needed.



Note: MSAccess does not have variables in SQL statements the way that SQL Server does.
 
Upvote 0

Forum statistics

Threads
1,221,831
Messages
6,162,248
Members
451,756
Latest member
tommyw

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