Like Month year Q

K

KAnoe

I have a report that the user would like to type in only the month and Year
in the following format: Jan 05. Then the report would show the report with
only dates for Jan 05. Only one month per report.

I know how to do a like query for just one date

Like [Enter the date for your report]

Thanks
 
J

John Vinson

I have a report that the user would like to type in only the month and Year
in the following format: Jan 05. Then the report would show the report with
only dates for Jan 05. Only one month per report.

I know how to do a like query for just one date

Like [Enter the date for your report]

Thanks

You're assuming that the date/time field is a text string. It isn't.

A date is stored as a Double Float count of days and fractions of a
day (times) since an arbitrary start date.

I'd suggest a criterion of
= CDate([Enter month and year, e.g. Jan 2005:]) AND < DateAdd("m", 1, [Enter month and year, e.g. Jan 2005:])

Access will fill in the first day of the month if the user enters only
the month and year. I think if they enter Jan 05, it will assume you
mean the 5th of January in the current year, though!

John W. Vinson[MVP]
 
6

'69 Camaro

Hi.

I know what you're thinking: If I just tweak John's suggestion a little
bit, I could give my users the ability to use a two-digit year and the query
won't assume that the two digits are the day of the month and assume the
current year. Here's what that "little tweak" would look like in order to
accommodate both two-digit and four-digit years:

SELECT *
FROM tblSales
WHERE ((SomeDate >= CDate(MID([Enter month and year:], 1, INSTR(1, [Enter
month and year:], " ")) & "1, " & IIF(INSTR(1, [Enter month and year:], " ")
= (LEN([Enter month and year:]) - 2), IIF (MID([Enter month and year:],
INSTR(1, [Enter month and year:], " ") + 1, 2) <= 50, "20", "19") &
MID([Enter month and year:], INSTR(1, [Enter month and year:], " ") + 1, 2),
MID([Enter month and year:], INSTR(1, [Enter month and year:], " ") + 1,
4)))) AND (SomeDate < DateAdd("m", 1, MID([Enter month and year:], 1,
INSTR(1, [Enter month and year:], " ")) & "1, " & IIF(INSTR(1, [Enter month
and year:], " ") = (LEN([Enter month and year:]) - 2), IIF (MID([Enter month
and year:], INSTR(1, [Enter month and year:], " ") + 1, 2) <= 50, "20", "19")
& MID([Enter month and year:], INSTR(1, [Enter month and year:], " ") + 1,
2), MID([Enter month and year:], INSTR(1, [Enter month and year:], " ") + 1,
4)))));

.... where tblSales is the name of the table and SomeDate is the name of the
field which contains the date within the month and year specified. This
query uses window bracketing to assume which century the two-digit year falls
into. This query is ugly and doubly difficult to maintain: it's hard to
make changes, and it uses window bracketing for the assumed year (which bit
many companies in the butt for Y2K) so is only good for the years 1951
through 2050.

The best advice you can receive is the advice John already gave you. Use
his suggestion for the WHERE clause and tell the users that they must use
four-digit years:

SELECT *
FROM tblSales
WHERE ((SomeDate >= CDate([Enter month and year:])) AND (SomeDate <
DateAdd("m", 1, [Enter month and year:])))

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.


KAnoe said:
I have a report that the user would like to type in only the month and Year
in the following format: Jan 05. Then the report would show the report with
only dates for Jan 05. Only one month per report.

I know how to do a like query for just one date

Like [Enter the date for your report]

Thanks
 
J

John Vinson

Here's what that "little tweak" would look like in order to
accommodate both two-digit and four-digit years:

<boggle>

What a tour de force, Gunny. Wow! I love it...

wouldn't USE it of course...


John W. Vinson[MVP]
 
Top