Hi everyone:
This is a module that would help you out with any type of date functions:Just copy and paste it into a module.
Option Compare Database
'A Little book of Month, Days, Coding
Function Convdt(ByVal dtsDate As Variant) As Variant
If Not IsDate(dtsDate) Then
Convdt = Null
Else
Convdt = CDate(dtsDate)
End If
End Function
Function EndOfMonth(xdate As Variant)
On Error Resume Next
If IsNull(xdate) Then
EndOfMonth = Null
Else
EndOfMonth = DateAdd("d", -1, DateAdd("m", 1, StartOfMonth(xdate)))
End If
End Function
Function GetBeginDateofQuarter(xdate As Variant) As Variant
'this function takes any date and returns the first day of that quarter
If IsNull(xdate) Then
GetBeginDateofQuarter = Null
Exit Function
End If
Dim tval As Variant
tval = DatePart("q", xdate)
tval = (tval * 3) - 2
tval = DateValue(tval & "/1/" & Year(xdate))
GetBeginDateofQuarter = tval
End Function
Function GetEndDateofQuarter(xdate As Variant) As Variant
'this function takes any date and returns the last day of the last month of that quarter
If IsNull(xdate) Then
GetEndDateofQuarter = Null
Exit Function
End If
Dim tval As Variant
tval = DatePart("q", xdate)
tval = (tval * 3)
tval = DateValue(tval & "/1/" & Year(xdate))
tval = EndOfMonth(tval)
GetEndDateofQuarter = tval
End Function
Function GetFirstDayLastMonthofQuarter(xdate As Variant) As Variant
'this function takes any date and returns the first day of the last month of that quarter
If IsNull(xdate) Then
GetFirstDayLastMonthofQuarter = Null
Exit Function
End If
Dim tval As Variant
tval = DatePart("q", xdate)
tval = (tval * 3)
tval = DateValue(tval & "/1/" & Year(xdate))
GetFirstDayLastMonthofQuarter = tval
End Function
Function GetLastQuartersQtrNum()
Dim tval As Variant
tval = Date
tval = DateAdd("q", -1, tval)
tval = DatePart("q", tval)
GetLastQuartersQtrNum = tval
End Function
Function GetLastQuartersYear()
Dim tval As Variant
tval = Date
tval = DateAdd("q", -1, tval)
tval = DatePart("yyyy", tval)
GetLastQuartersYear = tval
End Function
Function GetPrevDay(xdate As Variant) As Variant
GetPrevDay = DateAdd("y", -1, xdate)
End Function
Function StartOfMonth(xdate As Variant) As Variant
On Error Resume Next
If IsNull(xdate) Then
StartOfMonth = Null
Else
StartOfMonth = DateValue(Month(xdate) & "/1/" & Year(xdate))
End If
End Function
Function StartofNextMonth(xdate As Variant) As Variant
On Error Resume Next
Dim tdate As Variant
tdate = DateAdd("m", 1, xdate)
StartofNextMonth = DateValue(Month(tdate) & "/1/" & Year(tdate))
End Function
Function PreviousMonth(xdate As Variant) As Variant
On Error Resume Next
Dim tdate As Variant
If IsNull(xdate) Then
PreviousMonth = Null
Else
tdate = DateAdd("m", -1, xdate)
PreviousMonth = DateValue(Month(tdate) & "/1/" & Year(tdate))
End If
End Function
Function LMTD(xdate As Variant) As Variant
On Error Resume Next
If IsNull(xdate) Then
LMTD = Null
Else
tdate = DateAdd("m", -1, DateAdd("d", -1, xdate))
LMTD = DateValue(tdate)
End If
End Function
Public Function GetDays(ByVal dtsDate As Variant, ByVal lngday As Long) As Long
'sample call GetDays(Date(),4) returns number of Wednesdays in current month
'1 = sunday, 2 = monday, 3 = tues, 4 = wed, 5 = thrs, 6 = fri, 7 = sat
Dim dtsStart As Date
Dim dtsEnd As Date
Dim lngCount As Long
lngCount = 0
dtsStart = StartOfMonth(dtsDate)
dtsEnd = EndOfMonth(dtsDate)
Dim MyDate As Variant
MyDate = dtsStart
Do Until MyDate > dtsEnd
If WeekDay(MyDate) = lngday Then
lngCount = lngCount + 1
End If
MyDate = MyDate + 1
Loop
GetDays = lngCount
End Function
Public Function PastDays(ByVal dtsDate As Variant, ByVal lngday As Long) As Long
'sample call PastDays(Date(),4) returns number of Wednesdays in current month
'1 = sunday, 2 = monday, 3 = tues, 4 = wed, 5 = thrs, 6 = fri, 7 = sat
Dim dtsStart As Date
Dim dtsEnd As Date
Dim lngCount As Long
lngCount = 0
dtsStart = StartOfMonth(dtsDate)
dtsEnd = [Forms]![frmReportControl]![EndDate]
Dim MyDate As Variant
MyDate = dtsStart
Do Until MyDate > dtsEnd
If WeekDay(MyDate) = lngday Then
lngCount = lngCount + 1
End If
MyDate = MyDate + 1
Loop
PastDays = lngCount
End Function
Public Function H(ByVal dtsDate As Variant, ByVal lngday As Long) As Long
'sample call GetDays(Date(),4) returns number of Wednesdays in current month
'1 = sunday, 2 = monday, 3 = tues, 4 = wed, 5 = thrs, 6 = fri, 7 = sat
Dim dtsStart As Date
Dim dtsEnd As Date
Dim lngCount As Long
lngCount = 0
dtsStart = StartOfMonth(dtsDate)
dtsEnd = EndOfMonth(dtsDate)
Dim MyDate As Variant
MyDate = dtsStart
Do Until MyDate > dtsEnd
If WeekDay(MyDate) = lngday Then
If Not IsHoliday(MyDate) Then
lngCount = lngCount + 1
Else
'Do nothing, it's a holiday
End If
End If
MyDate = MyDate + 1
Loop
H = lngCount
End Function
Public Function Days(Optional dtsDate As Date = 0) As Integer
If dtsDate = 0 Then
dtsDate = Date
End If
Days = DateSerial(Year(dtsDate), Month(dtsDate) + 1, 1) - DateSerial(Year(dtsDate), Month(dtsDate), 1)
End Function
Public Function WholeMonth(ByVal dtsDate As Variant) As Variant
Dim dtsEnd As Date
Dim MyDate As Variant
dtsEnd = EndOfMonth(dtsDate)
MyDate = dtsDate
Do
Do Until MyDate > dtsEnd
MyDate = MyDate + 1
If MyDate < dtsEnd Then
WholeMonth = DateAdd("d", 1, MyDate)
Exit Do
End If
Loop
Debug.Print WholeMonth
Loop Until MyDate > dtsEnd
End Function
Public Function CompleteH(ByVal dtsDate As Variant, ByVal lngday As Long) As Long
'Gives you how many completed during the month minus holidays
'1 = sunday, 2 = monday, 3 = tues, 4 = wed, 5 = thrs, 6 = fri, 7 = sat
Dim dtsStart As Date
Dim dtsEnd As Date
Dim lngCount As Long
lngCount = 0
dtsStart = StartOfMonth(dtsDate)
dtsEnd = [Forms]![frmReportControl]![EndDate]
Dim MyDate As Variant
MyDate = dtsStart
Do Until MyDate > dtsEnd
If WeekDay(MyDate) = lngday Then
If Not IsHoliday(MyDate) Then
lngCount = lngCount + 1
Else
'Do nothing, it's a holiday
End If
End If
MyDate = MyDate + 1
Loop
CompleteH = lngCount
End Function
Function PrevMonth(xdate As Variant) As Variant
On Error Resume Next
Dim tdate As Variant
If IsNull(xdate) Then
PrevMonth = Null
Else
tdate = DateAdd("m", -1, xdate)
PrevMonth = Format(tdate, "mmm yyyy"
End If
End Function
Cool. Thanks
This is a module that would help you out with any type of date functions:Just copy and paste it into a module.
Option Compare Database
'A Little book of Month, Days, Coding
Function Convdt(ByVal dtsDate As Variant) As Variant
If Not IsDate(dtsDate) Then
Convdt = Null
Else
Convdt = CDate(dtsDate)
End If
End Function
Function EndOfMonth(xdate As Variant)
On Error Resume Next
If IsNull(xdate) Then
EndOfMonth = Null
Else
EndOfMonth = DateAdd("d", -1, DateAdd("m", 1, StartOfMonth(xdate)))
End If
End Function
Function GetBeginDateofQuarter(xdate As Variant) As Variant
'this function takes any date and returns the first day of that quarter
If IsNull(xdate) Then
GetBeginDateofQuarter = Null
Exit Function
End If
Dim tval As Variant
tval = DatePart("q", xdate)
tval = (tval * 3) - 2
tval = DateValue(tval & "/1/" & Year(xdate))
GetBeginDateofQuarter = tval
End Function
Function GetEndDateofQuarter(xdate As Variant) As Variant
'this function takes any date and returns the last day of the last month of that quarter
If IsNull(xdate) Then
GetEndDateofQuarter = Null
Exit Function
End If
Dim tval As Variant
tval = DatePart("q", xdate)
tval = (tval * 3)
tval = DateValue(tval & "/1/" & Year(xdate))
tval = EndOfMonth(tval)
GetEndDateofQuarter = tval
End Function
Function GetFirstDayLastMonthofQuarter(xdate As Variant) As Variant
'this function takes any date and returns the first day of the last month of that quarter
If IsNull(xdate) Then
GetFirstDayLastMonthofQuarter = Null
Exit Function
End If
Dim tval As Variant
tval = DatePart("q", xdate)
tval = (tval * 3)
tval = DateValue(tval & "/1/" & Year(xdate))
GetFirstDayLastMonthofQuarter = tval
End Function
Function GetLastQuartersQtrNum()
Dim tval As Variant
tval = Date
tval = DateAdd("q", -1, tval)
tval = DatePart("q", tval)
GetLastQuartersQtrNum = tval
End Function
Function GetLastQuartersYear()
Dim tval As Variant
tval = Date
tval = DateAdd("q", -1, tval)
tval = DatePart("yyyy", tval)
GetLastQuartersYear = tval
End Function
Function GetPrevDay(xdate As Variant) As Variant
GetPrevDay = DateAdd("y", -1, xdate)
End Function
Function StartOfMonth(xdate As Variant) As Variant
On Error Resume Next
If IsNull(xdate) Then
StartOfMonth = Null
Else
StartOfMonth = DateValue(Month(xdate) & "/1/" & Year(xdate))
End If
End Function
Function StartofNextMonth(xdate As Variant) As Variant
On Error Resume Next
Dim tdate As Variant
tdate = DateAdd("m", 1, xdate)
StartofNextMonth = DateValue(Month(tdate) & "/1/" & Year(tdate))
End Function
Function PreviousMonth(xdate As Variant) As Variant
On Error Resume Next
Dim tdate As Variant
If IsNull(xdate) Then
PreviousMonth = Null
Else
tdate = DateAdd("m", -1, xdate)
PreviousMonth = DateValue(Month(tdate) & "/1/" & Year(tdate))
End If
End Function
Function LMTD(xdate As Variant) As Variant
On Error Resume Next
If IsNull(xdate) Then
LMTD = Null
Else
tdate = DateAdd("m", -1, DateAdd("d", -1, xdate))
LMTD = DateValue(tdate)
End If
End Function
Public Function GetDays(ByVal dtsDate As Variant, ByVal lngday As Long) As Long
'sample call GetDays(Date(),4) returns number of Wednesdays in current month
'1 = sunday, 2 = monday, 3 = tues, 4 = wed, 5 = thrs, 6 = fri, 7 = sat
Dim dtsStart As Date
Dim dtsEnd As Date
Dim lngCount As Long
lngCount = 0
dtsStart = StartOfMonth(dtsDate)
dtsEnd = EndOfMonth(dtsDate)
Dim MyDate As Variant
MyDate = dtsStart
Do Until MyDate > dtsEnd
If WeekDay(MyDate) = lngday Then
lngCount = lngCount + 1
End If
MyDate = MyDate + 1
Loop
GetDays = lngCount
End Function
Public Function PastDays(ByVal dtsDate As Variant, ByVal lngday As Long) As Long
'sample call PastDays(Date(),4) returns number of Wednesdays in current month
'1 = sunday, 2 = monday, 3 = tues, 4 = wed, 5 = thrs, 6 = fri, 7 = sat
Dim dtsStart As Date
Dim dtsEnd As Date
Dim lngCount As Long
lngCount = 0
dtsStart = StartOfMonth(dtsDate)
dtsEnd = [Forms]![frmReportControl]![EndDate]
Dim MyDate As Variant
MyDate = dtsStart
Do Until MyDate > dtsEnd
If WeekDay(MyDate) = lngday Then
lngCount = lngCount + 1
End If
MyDate = MyDate + 1
Loop
PastDays = lngCount
End Function
Public Function H(ByVal dtsDate As Variant, ByVal lngday As Long) As Long
'sample call GetDays(Date(),4) returns number of Wednesdays in current month
'1 = sunday, 2 = monday, 3 = tues, 4 = wed, 5 = thrs, 6 = fri, 7 = sat
Dim dtsStart As Date
Dim dtsEnd As Date
Dim lngCount As Long
lngCount = 0
dtsStart = StartOfMonth(dtsDate)
dtsEnd = EndOfMonth(dtsDate)
Dim MyDate As Variant
MyDate = dtsStart
Do Until MyDate > dtsEnd
If WeekDay(MyDate) = lngday Then
If Not IsHoliday(MyDate) Then
lngCount = lngCount + 1
Else
'Do nothing, it's a holiday
End If
End If
MyDate = MyDate + 1
Loop
H = lngCount
End Function
Public Function Days(Optional dtsDate As Date = 0) As Integer
If dtsDate = 0 Then
dtsDate = Date
End If
Days = DateSerial(Year(dtsDate), Month(dtsDate) + 1, 1) - DateSerial(Year(dtsDate), Month(dtsDate), 1)
End Function
Public Function WholeMonth(ByVal dtsDate As Variant) As Variant
Dim dtsEnd As Date
Dim MyDate As Variant
dtsEnd = EndOfMonth(dtsDate)
MyDate = dtsDate
Do
Do Until MyDate > dtsEnd
MyDate = MyDate + 1
If MyDate < dtsEnd Then
WholeMonth = DateAdd("d", 1, MyDate)
Exit Do
End If
Loop
Debug.Print WholeMonth
Loop Until MyDate > dtsEnd
End Function
Public Function CompleteH(ByVal dtsDate As Variant, ByVal lngday As Long) As Long
'Gives you how many completed during the month minus holidays
'1 = sunday, 2 = monday, 3 = tues, 4 = wed, 5 = thrs, 6 = fri, 7 = sat
Dim dtsStart As Date
Dim dtsEnd As Date
Dim lngCount As Long
lngCount = 0
dtsStart = StartOfMonth(dtsDate)
dtsEnd = [Forms]![frmReportControl]![EndDate]
Dim MyDate As Variant
MyDate = dtsStart
Do Until MyDate > dtsEnd
If WeekDay(MyDate) = lngday Then
If Not IsHoliday(MyDate) Then
lngCount = lngCount + 1
Else
'Do nothing, it's a holiday
End If
End If
MyDate = MyDate + 1
Loop
CompleteH = lngCount
End Function
Function PrevMonth(xdate As Variant) As Variant
On Error Resume Next
Dim tdate As Variant
If IsNull(xdate) Then
PrevMonth = Null
Else
tdate = DateAdd("m", -1, xdate)
PrevMonth = Format(tdate, "mmm yyyy"
End If
End Function
Cool. Thanks