Question : Query Field With Date/Time

How do I change the expression below to correctly query a field where some values have both date and time and some values only have the date?  I don't want to have to use Format([MyDateTimeField], "mm/dd/yyyy") because I'm trying to avoid having to change the field name.  Also, I need it for "between last month" as well.

Between DateAdd("d",-7,Date()) and DateAdd("d",-1,Date())

Answer : Query Field With Date/Time

Sorry, I missed the second half of your question...

You can use simple arithmetic to add/subtract days.  This is faster (though perhaps less "kosher") than DateAdd:

DateValue([DateField]) Between Date()-7 and Date()-1

Use DateSerial for the month range:

DateValue([Datefield]) between DateSerial( Year(Date()), Month(Date())-1, 1) and DateSerial( Year(Date()), Month(Date()), 0)

This will give a range between the first day of the previous month and the zeroth day of the current month (ie last day of the previous month)

If you are processing many records, it may be faster to use a non-inclusive comparison, instead of between.  This means you will not need to call the DateValue function to process [DateField] for every record:

[DateField] >= Date()-7 and [DateField] < Date()

and, for the previous month:

[Datefield] >= DateSerial( Year(Date()), Month(Date())-1, 1) and [DateField] < DateSerial( Year(Date()), Month(Date()), 1)

Note that the upper end of the range is non-inclusive (< not <=) and uses the NEXT day.

--
Graham

Random Solutions  
 
programming4us programming4us