VBA Error 3075 When Filtering Between Dates - date

I know that there are a lot of answered questions regarding this topic but none of them answer the following question:
I have got this little and simple code:
Private Sub btnHwEOLNext2_Click()
Dim filterString As String
Dim startDate As Date
Dim endDate As Date
startDate = Date
endDate = Date + 730
filterString = "[HW End of Support] BETWEEN '" & startDate & " And " & endDate & "'"
Me.Filter = filterString
DoCmd.SetOrderBy "[HW End of Support] ASC"
Me.FilterOn = True
End Sub
all i am trying to do is to filter between 2 dates. the startDatedate will always be the day you use the form and the endDate will always be 730 days after startDate
why do i keep on getting
error 3075
? Help please!

If you put the date between # signs, then VBA considers it as a date.
As far SQL is using American date system with MM-DD-YYYY, you can change the format between the # signs, before parsing:
"[HW End] BETWEEN #" & Format(sd,"MM-DD-YYYY") & "# And #" & Format(ed,"MM-DD-YYYY") & "#"

Related

dlookup not working for other month than MAY

I am using dlookup (date = date) its fine when the date is of "May" but when I put date of any other month like 1/6/19 the dlookup remains silent.
in Dlookup if date is of MAY its fine but not working when date id of JUN the code I am using is as below
Me
Total = DLookup("Total", "Dailycash", "Cdate =#" & sdate & "#")
Apply a proper format to the date value expression:
Total = DLookup("Total", "Dailycash", "Cdate = #" & Format(sdate, "yyyy\/mm\/dd") & "#")

Create Dynamic List of Business Dates Since Arbitrary Start Date in Access

Using MS Access 2010, I need to generate a complete list of dates from an arbitrary start date, say #1/1/2015#, to now, Date(). I would like this list to live in its own table, although a query would work, too. I also would prefer to only grab business days/week days. Can anyone help?
I'm tracking business process errors by date. A new error record is made for each error, and each record is tagged with a date. However, there is not an error on every date. So reporting over time does not give a correct visualization, as the dates without errors are not represented.
I appreciate help generating this list in Access, as well as any alternative ideas for representing this information.
Cheers,
Burgess
Update - I've been able to make a list of dates since a start date. Here's my code:
Sub createDatesTable()
'Declare variables'
Dim startDate As Date
Dim endDate As Date
Dim countDate As Date
Dim length As Long
Dim i As Long
Dim dates() As Date
'Initialize'
startDate = #6/23/2015#
endDate = #9/1/2015#
countDate = startDate
length = endDate - startDate
'Define date array length'
ReDim dates(1 To length)
'Generate date list in array'
For i = 1 To (length)
dates(i) = countDate
countDate = countDate + 1
Next i
'Print array to Immediate Window'
For i = 1 To length
Debug.Print dates(i)
Next i
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("tblDateList", dbOpenDynaset)
'Add array data to existing table'
For i = 1 To length
rs.AddNew
rs!DateList = dates(i)
rs.Update
Next i
rs.Close
db.Close
End Sub
Now, I'm very new to VBA, so this may well contain elementary mistakes. Thanks for the feedback.
-Burgess
Your table can have a structure like this:
Column name | Data type
---------------+----------
dt | DateTime
isBusinessDay | Yes/No
And as for the filling, something like this:
public sub fillDates(startDate as date, endDate as date)
dim db as DAO.database, rs as DAO.Recordset
dim strSQL as string
dim d as date
strSQL = "delete from tbl_dates"
docmd.runsql strSQL
set db = currentdb()
set rs = db.openRecordset("tbl_dates", dbOpenDynaset, dbAppendOnly)
for d = startDate to endDate
with rs
.addNew
![dt] = d
![isBusinessDay] = (format(d, "w", vbMonday) <= 5)
.update
end with
next d
rs.close
db.close
end sub

Count X Amount of Working Days from Date Entered

I have a Microsoft Access database where the user is required to enter a Date Opened: value. Once entered, this triggers a calculation in another field, Deadline (25 WD):. This works via the following function in the latter field:
=DateAdd("d",25,[Date opened])
What I want to do, however, is to count 25 working days from the date entered in Date Opened:. I have a table holidays which contains a list of UK holidays up until 2020.
How can I merge to two, so-to-speak, in order to produce a valid Deadline (25 WD): value which does not count any of the dates listed in holidays?
For example, if the date entered is 01/01/2015, then the function would count 25 working days from 01/01/2015, meaning that it would exclude all weekends and any bank holidays that fall within that period and the resulting date value in the field Deadline (25 WD) will also be a working day (i.e. not a weekend or bank holiday).
You might need a UDF to get you through this. Something like,
Function addWorkDays(addNumber As Long, Date2 As Date) As Date
'********************
'Code Courtesy of
' Paul Eugin
'********************
Dim finalDate As Date
Dim i As Long, tmpDate As Date
tmpDate = Date2
i = 1
Do While i <= addNumber
If Weekday(tmpDate) <> 1 And Weekday(tmpDate) <> 7 And _
DCount("*", "tbl_BankHolidays", "bankDate = " & Format(tmpDate, "\#mm\/dd\/yyyy\#")) = 0 Then i = i + 1
tmpDate = DateAdd("d", 1, tmpDate)
Loop
Do While Weekday(tmpDate) = 1 Or Weekday(tmpDate) = 7 Or _
DCount("*", "tbl_BankHolidays", "bankDate = " & Format(tmpDate, "\#mm\/dd\/yyyy\#")) <> 0
tmpDate = DateAdd("d", 1, tmpDate)
Loop
addWorkDays = tmpDate
End Function
So, when you add 25 days to a date, it will skip all weekends and bank holidays stored in your table - tbl_BankHolidays.
? addWorkDays(25, Date())
25/06/2015
Hope this helps !
EDIT: I have added another loop to see if the end date falls on a bank holiday or weekend, if it does it will add one more day until it reaches a weekday.
You can use this function:
Public Function DateAddWorkdays( _
ByVal lngNumber As Long, _
ByVal datDate As Date, _
Optional ByVal booWorkOnHolidays As Boolean) _
As Date
' Adds lngNumber of workdays to datDate.
' 2014-10-03. Cactus Data ApS, CPH
' Calendar days per week.
Const clngWeekdayCount As Long = 7
' Workdays per week.
Const clngWeekWorkdays As Long = 5
' Average count of holidays per week maximum.
Const clngWeekHolidays As Long = 1
' Maximum valid date value.
Const cdatDateRangeMax As Date = #12/31/9999#
' Minimum valid date value.
Const cdatDateRangeMin As Date = #1/1/100#
Dim aHolidays() As Date
Dim lngDays As Long
Dim lngDiff As Long
Dim lngDiffMax As Long
Dim lngSign As Long
Dim datDate1 As Date
Dim datDate2 As Date
Dim datLimit As Date
Dim lngHoliday As Long
lngSign = Sgn(lngNumber)
datDate2 = datDate
If lngSign <> 0 Then
If booWorkOnHolidays = True Then
' Holidays are workdays.
Else
' Retrieve array with holidays between datDate and datDate + lngDiffMax.
' Calculate the maximum calendar days per workweek.
lngDiffMax = lngNumber * clngWeekdayCount / (clngWeekWorkdays - clngWeekHolidays)
' Add one week to cover cases where a week contains multiple holidays.
lngDiffMax = lngDiffMax + Sgn(lngDiffMax) * clngWeekdayCount
datDate1 = DateAdd("d", lngDiffMax, datDate)
aHolidays = GetHolidays(datDate, datDate1)
End If
Do Until lngDays = lngNumber
If lngSign = 1 Then
datLimit = cdatDateRangeMax
Else
datLimit = cdatDateRangeMin
End If
If DateDiff("d", DateAdd("d", lngDiff, datDate), datLimit) = 0 Then
' Limit of date range has been reached.
Exit Do
End If
lngDiff = lngDiff + lngSign
datDate2 = DateAdd("d", lngDiff, datDate)
Select Case Weekday(datDate2)
Case vbSaturday, vbSunday
' Skip weekend.
Case Else
' Check for holidays to skip.
' Ignore error when using LBound and UBound on an unassigned array.
On Error Resume Next
For lngHoliday = LBound(aHolidays) To UBound(aHolidays)
If Err.Number > 0 Then
' No holidays between datDate and datDate1.
ElseIf DateDiff("d", datDate2, aHolidays(lngHoliday)) = 0 Then
' This datDate2 hits a holiday.
' Subtract one day before adding one after the loop.
lngDays = lngDays - lngSign
Exit For
End If
Next
On Error GoTo 0
lngDays = lngDays + lngSign
End Select
Loop
End If
DateAddWorkdays = datDate2
End Function
Public Function GetHolidays( _
ByVal datDate1 As Date, _
ByVal datDate2 As Date, _
Optional ByVal booDesc As Boolean) _
As Date()
' Finds the count of holidays between datDate1 and datDate2.
' The holidays are returned as an array of dates.
' DAO objects are declared static to speed up repeated calls with identical date parameters.
' 2014-10-03. Cactus Data ApS, CPH
' The table that holds the holidays.
Const cstrTable As String = "tblHoliday"
' The field of the table that holds the dates of the holidays.
Const cstrField As String = "HolidayDate"
' Constants for the arrays.
Const clngDimRecordCount As Long = 2
Const clngDimFieldOne As Long = 0
Static dbs As DAO.Database
Static rst As DAO.Recordset
Static datDate1Last As Date
Static datDate2Last As Date
Dim adatDays() As Date
Dim avarDays As Variant
Dim strSQL As String
Dim strDate1 As String
Dim strDate2 As String
Dim strOrder As String
Dim lngDays As Long
If DateDiff("d", datDate1, datDate1Last) <> 0 Or DateDiff("d", datDate2, datDate2Last) <> 0 Then
' datDate1 or datDate2 has changed since the last call.
strDate1 = Format(datDate1, "\#yyyy\/mm\/dd\#")
strDate2 = Format(datDate2, "\#yyyy\/mm\/dd\#")
strOrder = Format(booDesc, "\A\s\c;\D\e\s\c")
strSQL = "Select " & cstrField & " From " & cstrTable & " " & _
"Where " & cstrField & " Between " & strDate1 & " And " & strDate2 & " " & _
"Order By 1 " & strOrder
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
' Save the current set of date parameters.
datDate1Last = datDate1
datDate2Last = datDate2
End If
lngDays = rst.RecordCount
If lngDays = 0 Then
' Leave adatDays() as an unassigned array.
Else
ReDim adatDays(lngDays - 1)
' As repeated calls may happen, do a movefirst.
rst.MoveFirst
avarDays = rst.GetRows(lngDays)
' rst is now positioned at the last record.
For lngDays = LBound(avarDays, clngDimRecordCount) To UBound(avarDays, clngDimRecordCount)
adatDays(lngDays) = avarDays(clngDimFieldOne, lngDays)
Next
End If
' DAO objects are static.
' Set rst = Nothing
' Set dbs = Nothing
GetHolidays = adatDays()
End Function

Classic ASP - Display all dates within a certain date range

I was wondering if i could get some help.
I have a little script that gets a date range based on a persons input.
It outputs the date range like so
6/7/2014
6/27/2014
What i need to do is output each date between these 2 dates.
Any help would be greatly appreciated.
Cheers
Dates in Classic ASP are fairly easy to use. Try this first: http://www.classicasp.aspfaq.com/date-time-routines-manipulation/could-i-get-a-little-help-with-dates.html
Here's a demo of looping through a series of sequential dates:
dim dcount, newdate
dcount = 0
newdate = startdate
do
response.write(newdate & "<br />")
newdate = newdate + 1
loop until newdate > stopdate
Obviously you'd need to specify startdate and stopdate, though this could be done as a sub or function.

Date Format changes when inserting into table if start date and end date cross one or more months

I have an Access 2013 form which has two unbound date fields, FromDate and ToDate. I insert these into a table (TblGuestBooking) which has an autonumber key field, so this doesn't feature in the SQL statement to follow.
If the FromDate and ToDate are in the same month, the dates are entered as dd/mm/yy, the format of the form field. If, however, the From date is in one month and the to date is in the next month or later month, the format changes to mm/dd/yy for the subsequent months.
for example 26/2/14 to 3/3/14 results in the following table entries:
26/02/14,
27/02/14,
28/02/14,
03/01/14,
03/02/14,
03/03/14
This is the code snippet I am using to put the dates into the table (BookingID is obtained from the form.)
Dim BookingDate As Date
Dim SQLString As String
....
BookingDate = FromDate
Do
SQLString = "INSERT INTO TblGuestBooking ([BookingDate], [BookingID]) VALUES (#" & BookingDate & "#" & "," & Me.GuestSuiteBookingID & ")"
DoCmd.SetWarnings False
DoCmd.RunSQL SQLString
DoCmd.SetWarnings True
BookingDate = BookingDate + 1
Loop Until BookingDate = ToDate + 1
If you've read this far, thank you for your time. If you can help me out, many, many thanks.
When processing date literals (text values enclosed by "hash marks" #) Access SQL will always interpret ambiguous xx-yy-zzzz dates as mm-dd-yyyy, regardless of the regional setting in place on the computer. So, if your machine is configured to display short dates as dd-mm-yyyy and you create an Access query that uses #04-02-2014# it will always be interpreted as April 2, not February 4.
The solution is to always format date literals as unambiguous yyyy-mm-dd values. In your case, instead of
... VALUES (#" & BookingDate & "#" ...
you would use something like
... VALUES (#" & Format(BookingDate, "yyyy-mm-dd") & "#" ...
In my case I've used following code for an ASP page that create and update a Date field in Access database.
strDay = Request("Day")
strMonth = Request("Month")
strYear = Request("Year")
InputDate = strYear & "-" & strMonth & "-" & strDay
if IsDate(InputDate) Then
InsertDate = CDate(InputDate)
else
'if date typed wrong, use Todays Date
InsertDate = Date()
end if
SQLUPD = "UPDATE MyDataBase SET DateField = '"& FormatDateTime(InsertDate, vbShortDate) &"'"
SQLINS = "INSERT INTO MyDataBase (DateField, Alist, Atype, Acomment) VALUES ('"& FormatDateTime(InsertDate, vbShortDate) &"', .....
Then all sorting and output opration with dates are going well.