Register  Login
Snippets & Tips
***NEW CUSTOMER OFFER***
Get 15 minutes of free Live Help!
Coupon Code: 20110729  
Live Help
Search Snippets & Tips Minimize
  

Notes

Some operating systems block files that come from other computers.  If you cannot get a downloaded file to work properly, right-click over the file, select the Properties option, and then click the Unblock button at the bottom of the General tab.

  

View Code Snippets & Tips Minimize
Apr 29

Written by: Extra Mile Data
4/29/2009 11:39 AM 

This VBA function passes back the last date that a particular weekday occurred.

Code:

Public Function LastWeekDayDate(CurrentDate, LastWeekDay)
' This procedure calculates the date of the last occurance of the day of the
' week represented by LastWeekDay.  For example, if today is Wednesday, with
' CurrentDate=12/17/08, and you would like last Friday's date (LastWeekDay=6),
' the result would be 12/12/08.  It returns a Null if there is a problem.

' LastWeekDay values:
' Sunday = 1
' Monday = 2
' Tuesday = 3
' Wednesday = 4
' Thursday = 5
' Friday = 6
' Saturday = 7

' LastWeekDayDate() Version 1.0.0
' Copyright © 2009 Extra Mile Data,
www.extramiledata.com.
' For questions or issues, please contact
support@extramiledata.com.
' Use (at your own risk) and modify freely as long as proper credit is given.

On Error GoTo Err_LastWeekDayDate
   
    Dim dteTemp As Date

    ' If CurrentDate is not a date, then pass back a Null.
    If Not IsDate(CurrentDate) Then
        LastWeekDayDate = Null
        GoTo Exit_LastWeekDayDate
    End If
   
    ' Initialize the date were are starting with.
    dteTemp = CurrentDate
   
    ' Subtract a day until the week day = LastWeekDay.
    Do Until WeekDay(dteTemp) = LastWeekDay
        dteTemp = DateAdd("d", -1, dteTemp)
    Loop
   
    ' Pass back that value.
    LastWeekDayDate = dteTemp

Exit_LastWeekDayDate:
    On Error Resume Next
    Exit Function

Err_LastWeekDayDate:
    MsgBox Err.Number & " " & Err.DESCRIPTION, vbCritical, "LastWeekDayDate()"
    LastWeekDayDate = Null
    Resume Exit_LastWeekDayDate

End Function

Download Code: basLastWeekDay.zip

Tags:

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 
  

Extra Mile Data - Go Faster...Go Further!

Go Faster...Go Further!  Call (479) 524-8479.
Extra Mile Data, your source for Microsoft Access help

Terms Of Use | Privacy Statement | © 2003-2011 Extra Mile Data