How To: Build MS Access query to return date of 2nd Saturday from Current Date? - ms-access

I'm trying to build a query or VBA code (which ever works best) to return the date for the 2nd Saturday from the current date.
Example: Today is Friday, February 3rd, 2023 (02/03/23). The automatic query should return Saturday, February 11th. This would be calculating the firstdayofweek argument used in the WEEKDAY Function (used in MS Excel and Access) based on the current date.
I've just not been able to build the correct query where it calculates this specific date in time based on the current date.
Any help would be greatly appreciated!
Thanks,
Matt

The structure is going to be '15 days after the friday before today' (originally I thought '14 days after the saturday before today', but that misfires if called on saturdays)
In VBA:
Function SecondSat(Optional DateFrom As Date) As Date
If DateFrom = 0 Then DateFrom = Date 'default to today's date
SecondSat = DateFrom - DatePart("w", DateFrom, vbSaturday) + 15
End Function
in MS Access query language:
SELECT Date() - DATEPART('w', Date(), 7) + 15

You can use my generic function DateNextWeekday found in my library at GitHub: VBA.Date
In code:
NextNextSaturday = DateAdd("ww", 1, DateNextWeekday(Date, vbSaturday))
In a query:
NextNextSaturday: VDateAdd("ww",1,DateNextWeekday(Date(),7))

Related

How to set a default parameter date for the beginning of the month to yesterday date

I have build an SSRS report. I set a default date parameters as following:
startdate : take the first day of the month.
EndDate : take yesterday date.
This code for the beginning of the month
=DateSerial(Year(Now()), Month(Now()), "1").AddMonths(0)
This code for yesterdate date
=DateAdd(DateInterval.Day,-1,CDate(FormatDateTime(Now,DateFormat.ShortDate)))
The date works perfectly until, a new month comes, the result will be like this:
startdate: 01. 5 2019
end date: 30. 4 2019
where it should be :
startdate: 01. 4 2019
end date: 30. 4 2019
how can i make sure if a new date comes , it will take the first day of the previous month.
You should add an IIF statement to check if today is the first day of the month. The following expression should do what you need.
=IIF(DatePart(DateInterval.Day, Today()) = 1,
DateSerial(Year(Now()), Month(Now())-1, 1),
DateSerial(Year(Now()), Month(Now()), 1))
Just change your StartDate to reference yesterday's date:
=DateSerial(Year(Today.AddDays(-1)),Month(Today.AddDays(-1)),1)

Query to return data for the past 6 months

I'm having some troubles in trying to figure out a date criteria in a query. Basically, I'm trying to get all the data from the past 6 months. For example, the current month is December 2017; I would like the query to return data ranging from June to December 2017. I've tried the following criteria:
Between Date() and DateAdd ("M", -6, Date())
However, the criteria returns data based on the day of the date; if the current date is 2 December 2017, the query returns dates from 2 June to 2 December 2017. I want the query to return data from the whole month of June (therefore if we're following the previous example, data from 1 June 2017 will be included too) to December. How do I go about achieving this?
The problem you're having sounds quite simple. You really just need to use the 1st day of the current month, instead of the current date.
There are many ways to get the first day of the current month, for example:
Date() - Day(Date()) + 1
While there are alternate ways to do this, try not to rely on casting a date to a string and back for performance/locale incompatibility
If you use this approach, your SQL WHERE would be:
Between Date() - Day(Date()) + 1 and DateAdd ("M", -6, Date() - Day(Date()) + 1)

SSRS date parameter : report start date should be yestardays on daily bases but on monday it should be friday's date

In my report I want to populate date parameter automatically. on daily bases date need to be select previous day (today()-1) , but on Monday it need to select Friday date.
please help me to write a function around this
I have date parameter in my report.
You can achieve this using Weekday(). Make sure the parameter has the Date/Time data type, and use this expression as the default value:
=DateAdd(DateInterval.Day,
IIF( Weekday(Today(),0) = 1, -3, -1),
Today())
The function Weekday(Today(),0) will equal 1 when today's date is a Monday. If true, you subtract three days from today's date to get the previous Friday. If not, you subtract 1 to get yesterday.

MS Access: Using a Date Between a Start Date and End Date for a Query

Question: Create a query that shows a listing of all kennels that were occupied on February 14, 2015.
So what I have is the Kennel ID, Start Date (of being in a kennel) and End Date (of being in the kennel)
I'm not sure how I am supposed to extract the date February 14, 2015 from being in between Start Date and End Date.
Any help will be appreciated
There are a number of ways of querying this depending on your end goal.
Have a read here for a good visual representation of how to get the results http://www.baldyweb.com/OverLap.htm
In a simple query hard coded to one date this would equate to Criteria like
[End Date] >= #02/14/2017# AND [Start Date] <= #02/14/2017#

Datediff function adds startdate day also to the output

I have a report in Report Builder 1.0 wherein I have written a formula that calculates the number of days between startdate and enddate. For that I have used a DATEDIFF function in such a manner:
DATEDIFF(DAY, TODAY(), column from table which has end date)
Suppose, if today's date is 1 July 2013 & the column is sending end date as 10 July 2013, then the DATEDIFF function is returning "10 days" whereas it should return output as value "9 days". Why is it returning 10 days rather than 9?
Execute this in Sql Server and you get 9 days:
select DATEDIFF(day, '2013-07-01', '2013-07-10')
Using this as a dataset:
select CAST('2013-07-01' AS DateTime) AS StartDate, CAST('2013-07-10 23:59:59' AS DateTime) AS EndDate
then using this expression in SSRS:
=DateDiff(DateInterval.Day, Fields!StartDate.Value, Fields!EndDate.Value)
still yields 9 days, even when using a time component. Try the following expression:
=DateDiff(DateInterval.Day, DateTime.Today, Fields!EndDate.Value)
Are you displaying the date fields to make sure what you think you should get is what you actually get? For instance, make sure you aren't being returned cached data.