Query to grab records in current month only

T

tsison7

I need a query that will grab records in the current month only. I know how
to do this in visual basic using datepart("m",[recorddate]) =datepart
("m",now()) but how can I do it in the query?
 
R

Rick Brandt

tsison7 said:
I need a query that will grab records in the current month only. I
know how to do this in visual basic using datepart("m",[recorddate])
=datepart ("m",now()) but how can I do it in the query?

WHERE DateField >= DateSerial(Year(Date()), Month(Date()), 1)
AND DateField < DateSerial(Year(Date()), Month(Date()) + 1, 1)
 
S

Steve Schapel

Tsison,

Your Visual Basic example will only work if the data only covers the
current year, otherwise you will get records for the same month in other
years. If this is the case, in Access you can do...
Month([recorddate])=Month(Date())
Otherwise...
Format([recorddate],"yyyymm")=Format(Date(),"yyyymm")
 
T

tsison7

Thanks Rick,

Before your post I added two extra fields to grab the month and the year and
filtered it that way using datepart. But your suggestion is much cleaner.
--
TIA


Rick Brandt said:
tsison7 said:
I need a query that will grab records in the current month only. I
know how to do this in visual basic using datepart("m",[recorddate])
=datepart ("m",now()) but how can I do it in the query?

WHERE DateField >= DateSerial(Year(Date()), Month(Date()), 1)
AND DateField < DateSerial(Year(Date()), Month(Date()) + 1, 1)
 
T

tsison7

I realized that once I ran the query. I went ended up using the dateserial
function instead.

Thanks,
--
TIA


Steve Schapel said:
Tsison,

Your Visual Basic example will only work if the data only covers the
current year, otherwise you will get records for the same month in other
years. If this is the case, in Access you can do...
Month([recorddate])=Month(Date())
Otherwise...
Format([recorddate],"yyyymm")=Format(Date(),"yyyymm")

--
Steve Schapel, Microsoft Access MVP
I need a query that will grab records in the current month only. I know how
to do this in visual basic using datepart("m",[recorddate]) =datepart
("m",now()) but how can I do it in the query?
 

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

Similar Threads


Top