I currently have parameters for reports for start/end dates with a defined
format:
SELECT *
FROM [Minor Injury]
WHERE ((([Minor Injury].Date)>=[Enter start date for report: 00/00/00] And
([Minor Injury].Date)<=[Enter end date for report: 00/00/00]) AND (([Minor
Injury].Dept)="AdTech Safety Team"))
ORDER BY [Minor Injury].Date;
I would like to have the start date default to the first day of the year and
the ending date to be the current date (now). How do I reword the SQL string
to get those values defaulted in?
1) Date is a reserved Access/VBA/Jet word and should not be used as a
field name.
For additional reserved words, see the Microsoft KnowledgeBase article
for your version of Access:
109312 'Reserved Words in Microsoft Access' for Access 97
209187 'ACC2000: Reserved Words in Microsoft Access'
286335 'ACC2002: Reserved Words in Microsoft Access'
321266 'ACC2002: Microsoft Jet 4.0 Reserved Words'
For an even more complete list of reserved words, see:
http://www.allenbrowne.com/AppIssueBadWord.html
2) Be careful in wanting to use Now() to return records. Now()
includes the time of day and therefore might return different records
depending upon the time of day you run the query. If the Date AND Time
is not important, use Date() instead.
3)You can use this.
For brevity I've changed the parameter text (the text within the
brackets) to [StartDate] and [EndDate]. You can substitute your own
text. I've also used [FieldName] Between .... And. You can use your
[FieldName] >= and [FieldName] <= syntax if you wish.
PARAMETERS [StartDate] DateTime, [EndDate] DateTime;
SELECT [Minor Injury].*
FROM [Minor Injury]
WHERE ([Minor Injury].ADate Between
IIf(IsNull([StartDate]),DateSerial(Year(Date()),1,1),[StartDate]) And
IIf(IsNull([EndDate]),Date(),[EndDate])) And [Minor Injury].Dept) =
"AdTech Safety Team"
ORDER BY [Minor Injury].ADate;
Change ADate to whatever the new Date Field Name is.
Watch the parenthesis placement.
Note the Parameters declaration statement.