[AccessD] Date calculation code

Joshua B jbusergroups at optushome.com.au
Thu Feb 27 17:33:00 CST 2003


Hello Joe Rojas,
This function should do what your after. Keep in mind that whichDay needs to be 1 for Sunday, 2 for Monday, etc. If you pass an invalid query (eg, the thirteenth Sunday of February), it will error and return 12:00:00 AM.

'===========
'Code Start
Function getDate(dte As Date, whichDay As Long, whichNumber As Long) As Date
Dim monthStart As Date
Dim firstDay As Date
Dim tmpDate As Date

monthStart = DateSerial(DatePart("yyyy", dte), DatePart("m", dte), 1)
tmpDate = monthStart
'Find the first Day
Do While DatePart("m", dte) = DatePart("m", tmpDate)
    If Weekday(tmpDate) = whichDay Then
        firstDay = tmpDate
        Exit Do
    End If
    tmpDate = DateAdd("d", 1, tmpDate)
Loop

getDate = DateAdd("d", ((whichNumber - 1) * 7), tmpDate)

If DatePart("m", dte) <> DatePart("m", getDate) Then
    MsgBox "The day requested does not exist!", vbCritical, "Error."
    getDate = 0
End If
End Function
'Code End
'=========

Hope this helps

======= At 2003-02-27, 14:15:00 you wrote: =======

>Hi All,
>
>	Does anyone have code that they can share that performs date
>calculations that return dates for inquiries like "last Thursday of a
>month", "second Monday of a month", "fourth Tuesday of a month", etc...
>
>Thanks!
>
>Joe Rojas

= = = = = = = = = = = = = = = = = = = =

Best regards.				 
Joshua B
jbusergroups at optushome.com.au
2003-02-28






More information about the AccessD mailing list