In MS Access I am trying to filter to show only records from previous week.
I have colums showing weeknumber based by record date.
Using DatePart("ww",Date(),2,2)-1 in criteria field works well if current week is not 1. It does not work to previous year.
How I can show only records from previous week or 2 weeks, even if it goes to previous year?
For the most recent week of data, start with something like this …
That approach will not break when your target YourDateField range include dates from 2 years. Another advantage of that approach is the query can make use of an index on YourDateField, if you have one. In that case the db engine can examine the index to find which rows match your criterion and read only the matching rows instead of all rows in the record source. (If you don’t have an index on YourDateField, add one to see if it speeds up the query significantly. You can drop the index if it’s ineffective.)
Conversely using a WHERE condition based on the
DatePart()function will force the db engine to examine each and every row in the query’s record source. That can be a performance bottleneck which can be significant with a huge table, especially if it is a linked table which requires data be read across a network connection.Edit: I misunderstood what you wanted. For the the week which started on the previous Monday, I used this condition:
The db engine will evaluate those expressions one time only when running the query, so while this approach is more complicated than what I first suggested, it can still offer the potential performance improvements over using
DatePart()for every row in the data source.