Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help writing a unique date function 1

Status
Not open for further replies.

Sdaddy

MIS
Nov 6, 2002
41
0
0
US
I am trying to write a function that will return the week number of a month. For instance Week 1, Week 2...I have tried writing an if statement, but I get a funky date in return. This is for a week over week view in MS Access on a report. I need the function or expression or conditional macro to make , for example 11/01/2002-11/02/2002, would be week 1", 11-03-11-09 would be "week 2" and so forth. I would use the on format macro(or anything that works I suppose. My last resort is to make a table and link it on the query level. That will introduce human error however. I greatly appreciate any help in this.
 
Try using the DatePart function. Use the 'y' argument for the interval.

This returns the Day of Year.

Then, you could divide by 7 and discard the remainder to get the week number.

Try this test sub:

Sub test()
Dim test

test = DatePart("y", "7/11/2002")
Debug.Print "Week"; test \ 7
test = DatePart("y", "8/11/2002")
Debug.Print "Week"; test \ 7
test = DatePart("y", "14/11/2002")
Debug.Print "Week"; test \ 7
test = DatePart("y", "7/1/2002")
Debug.Print "Week"; test \ 7
test = DatePart("y", "30/12/2002")
Debug.Print "Week"; test \ 7
End Sub

I get this output:

Week 44
Week 44
Week 45
Week 1
Week 52

HTH
 
Try this:

Code:
Function WeekOfYear(ByVal datDate As Date) As Byte
  WeekOfYear = DatePart("ww", Date)
End Function

Sample call:
Code:
myWeek = WeekOfYear("2/23/2002")
VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 
[hammer] Oops, correction below:
Code:
Function WeekOfYear(ByVal datDate As Date) As Byte
  WeekOfYear = DatePart("ww",
datDate
Code:
)
End Function
VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 
I re-read the post and I think this is more to your question:

Code:
Function WeekOfYear(ByVal datDate As Date) As String
  WeekOfYear = "Week " & DatePart("ww", datDate)
End Function
VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 
I can't believe I missed the ww argument!!!!
 
I greatly appreciate your help. This is along the lines of what I am looking for... but I am looking for the week number as it relates to a particular month.. rather than to the year.
 
Oh, how about this:
Code:
Function WeekOfMonth(ByVal datDate As Date) As String
  WeekOfMonth = "Week " & DatePart("ww", datDate) - _
                          DatePart("ww", Month(datDate) & _
                          "/1/" & Year(datDate)) + 1
End Function

VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 
It works... your are the man(unless you are a woman)!!!!!

thanks!!!

One more challenge I'm having is that for the weeks that begin in one month but end in another, Is there a way to make those dates show up as for instance 10-27 to 10-31 as week 5, rather than week 1.


thanks again!!!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top