Reputation: 41
I am creating a simple database for tracking working hours. The idea is that:
Calculation of working days should take into account weekends (saturday, sunday) and holidays from Holiday table.
I took a function sample from MSDN (Counting the Number of Working Days in Access 2007) and put it into a module in my MS Access 2010 db but each time I run a query I have this error.
Typically the same error appears attempting to run a query in another sample database from somewhere.
The problem is in the strWhere clause:
strWhere = "[Holiday] >=#" & startDate & "# AND [Holiday] <=#" & endDate & "#"
' Count the number of holidays.
nHolidays = DCount(Expr:="[Holiday]", Domain:=strHolidays, Criteria:=strWhere)
Workdays = nWeekdays - nHolidays
The error msg from both databases is available in the link below
Runtime Error 3075 Syntax error in date in query expression
Any help is appreciated.
Upvotes: 0
Views: 2329
Reputation: 55806
You must force a format on the string expressions for your dates. Get used to use the ISO sequence yyyy-mm-dd as it works everywhere:
strHolidays = "NameOfYourTable"
strWhere = "[Holiday] >= #" & Format(startDate, "yyyy\/mm\/dd") & "# AND [Holiday] <= #" & Format(endDate, "yyyy\/mm\/dd") & "#"
' Count the number of holidays.
nHolidays = DCount("*", strHolidays, strWhere)
Upvotes: 2
Reputation: 25252
Make sure the date is in MM/DD/YYYY order in VBA. Always. I generally use:
strWhere = "[Holiday] >= " & Format(startDate,"\#mm\/dd\/yyyy\#")
the second argument of DCount is strHolidays. That does not look like the name of a table/query. This argument should be the name of a table/query.
Upvotes: 0
Reputation: 7434
In the past I have had issues where VBA doesn't always respect the regional date settings. Try forcing it into US format before concatenating it
strWhere = "[Holiday] >=#" & Format(startDate, "MM/dd/yyyy") & "# AND [Holiday] <=#" & Format(endDate, "MM/dd/yyyy") & "#"
Upvotes: 0