G
Guest
I work at company where we have to run queries every month where the dates
have to be changed manually. For most of these reports the date is always
the previous months fist day, last day or some such relationship. I came up
with these functions to avoid have to manually change the dates in 50
different places each month. The only problem is that for some reason the
queries run MUCH slower using these functions instead of typing in the
dates.(It's a very large database) Is there anything I can do? I seems like
a huge wast of time to type these dates into the queries each month and also
subject to human error. Any help greatly appreciated.
Thanks,
Billy
Dallas,TX
Option Compare Database
Public Function EOPrevMonth()
'''''''''''''''''''''''''''''''''''''''''''''
'' Returns Last date of the previous month ''
'''''''''''''''''''''''''''''''''''''''''''''
Dim PriorMonth As Integer
Dim NewYear As Integer
PriorMonth = Month(DateAdd("m", -1, Date))
PMYear = Year(DateAdd("m", -1, Date))
Select Case PriorMonth
Case 1, 3, 5, 7, 8, 10, 12
EOPrevMonth = DateSerial(PMYear, PriorMonth, 31)
Case 4, 6, 9, 11
EOPrevMonth = DateSerial(PMYear, PriorMonth, 30)
Case 2
If Int(NewYear / 4) = NewYear / 4 Then
EOPrevMonth = DateSerial(PMYear, PriorMonth, 29)
Else
EOPrevMonth = DateSerial(PMYear, PriorMonth, 28)
End If
End Select
End Function
Public Function BOPrevMonth()
''''''''''''''''''''''''''''''''''''''''''
'' Returns First Date of Previous Month ''
''''''''''''''''''''''''''''''''''''''''''
BOPrevMonth = Format(DateAdd("m", -1, Date), "mm" & "/1/" & "YYYY")
End Function
Public Function BOPrevQtr()
''''''''''''''''''''''''''''''''''''''''''''
'' Returns First Date of Previous Quarter ''
''''''''''''''''''''''''''''''''''''''''''''
Dim Month As Integer
Dim CurYear As Integer
Month = CInt(Format(Date, "mm"))
CurYear = CInt(Format(Date, "yyyy"))
Select Case Month
Case 1 To 3
BOPrevQtr = "10/01/" & (CStr(CurYear - 1))
Case 4 To 6
BOPrevQtr = "01/01/" & (CStr(CurYear))
Case 7 To 9
BOPrevQtr = "04/01/" & (CStr(CurYear))
Case 10 To 12
BOPrevQtr = "07/01/" & (CStr(CurYear))
End Select
End Function
Public Function EOPrevQtr()
'''''''''''''''''''''''''''''''''''''''''''
'' Returns Last Date of Previous Quarter ''
'''''''''''''''''''''''''''''''''''''''''''
Dim Month As Integer
Dim CurYear As Integer
Month = CInt(Format(Date, "mm"))
CurYear = CInt(Format(Date, "yyyy"))
Select Case Month
Case 1 To 3
EOPrevQtr = "12/31/" & (CStr(CurYear - 1))
Case 4 To 6
EOPrevQtr = "03/31/" & (CStr(CurYear))
Case 7 To 9
EOPrevQtr = "06/30/" & (CStr(CurYear))
Case 10 To 12
EOPrevQtr = "09/30/" & (CStr(CurYear))
End Select
End Function
Public Function BOCurMonth()
BOCurMonth = Format(Date, "mm" & "/01/" & "yyyy")
End Function
Public Function BOTwoMonthsAgo()
''''''''''''''''''''''''''''''''''''''''''
'' Returns First Date of Two Months Ago ''
''''''''''''''''''''''''''''''''''''''''''
BOTwoMonthsAgo = Format(DateAdd("m", -2, Date), "mm" & "/1/" & "YYYY")
End Function
have to be changed manually. For most of these reports the date is always
the previous months fist day, last day or some such relationship. I came up
with these functions to avoid have to manually change the dates in 50
different places each month. The only problem is that for some reason the
queries run MUCH slower using these functions instead of typing in the
dates.(It's a very large database) Is there anything I can do? I seems like
a huge wast of time to type these dates into the queries each month and also
subject to human error. Any help greatly appreciated.
Thanks,
Billy
Dallas,TX
Option Compare Database
Public Function EOPrevMonth()
'''''''''''''''''''''''''''''''''''''''''''''
'' Returns Last date of the previous month ''
'''''''''''''''''''''''''''''''''''''''''''''
Dim PriorMonth As Integer
Dim NewYear As Integer
PriorMonth = Month(DateAdd("m", -1, Date))
PMYear = Year(DateAdd("m", -1, Date))
Select Case PriorMonth
Case 1, 3, 5, 7, 8, 10, 12
EOPrevMonth = DateSerial(PMYear, PriorMonth, 31)
Case 4, 6, 9, 11
EOPrevMonth = DateSerial(PMYear, PriorMonth, 30)
Case 2
If Int(NewYear / 4) = NewYear / 4 Then
EOPrevMonth = DateSerial(PMYear, PriorMonth, 29)
Else
EOPrevMonth = DateSerial(PMYear, PriorMonth, 28)
End If
End Select
End Function
Public Function BOPrevMonth()
''''''''''''''''''''''''''''''''''''''''''
'' Returns First Date of Previous Month ''
''''''''''''''''''''''''''''''''''''''''''
BOPrevMonth = Format(DateAdd("m", -1, Date), "mm" & "/1/" & "YYYY")
End Function
Public Function BOPrevQtr()
''''''''''''''''''''''''''''''''''''''''''''
'' Returns First Date of Previous Quarter ''
''''''''''''''''''''''''''''''''''''''''''''
Dim Month As Integer
Dim CurYear As Integer
Month = CInt(Format(Date, "mm"))
CurYear = CInt(Format(Date, "yyyy"))
Select Case Month
Case 1 To 3
BOPrevQtr = "10/01/" & (CStr(CurYear - 1))
Case 4 To 6
BOPrevQtr = "01/01/" & (CStr(CurYear))
Case 7 To 9
BOPrevQtr = "04/01/" & (CStr(CurYear))
Case 10 To 12
BOPrevQtr = "07/01/" & (CStr(CurYear))
End Select
End Function
Public Function EOPrevQtr()
'''''''''''''''''''''''''''''''''''''''''''
'' Returns Last Date of Previous Quarter ''
'''''''''''''''''''''''''''''''''''''''''''
Dim Month As Integer
Dim CurYear As Integer
Month = CInt(Format(Date, "mm"))
CurYear = CInt(Format(Date, "yyyy"))
Select Case Month
Case 1 To 3
EOPrevQtr = "12/31/" & (CStr(CurYear - 1))
Case 4 To 6
EOPrevQtr = "03/31/" & (CStr(CurYear))
Case 7 To 9
EOPrevQtr = "06/30/" & (CStr(CurYear))
Case 10 To 12
EOPrevQtr = "09/30/" & (CStr(CurYear))
End Select
End Function
Public Function BOCurMonth()
BOCurMonth = Format(Date, "mm" & "/01/" & "yyyy")
End Function
Public Function BOTwoMonthsAgo()
''''''''''''''''''''''''''''''''''''''''''
'' Returns First Date of Two Months Ago ''
''''''''''''''''''''''''''''''''''''''''''
BOTwoMonthsAgo = Format(DateAdd("m", -2, Date), "mm" & "/1/" & "YYYY")
End Function