943,654 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Mar 4th, 2009
-1

How to get Current Week Number of the Month

Expand Post »
Hello !
I want to get the current week number of the month. I have used this code below : -

Private Sub Command1_Click()
Dim t_Day As Double
t_Day = Day(DTPicker1.Value)
Select Case t_Day
Case 1, 2, 3, 4, 5, 6, 7, 8
Text1.Text = "1st Week"
Case 9, 10, 11, 12, 13, 14, 15, 16
Text1.Text = "2nd Week"
Case 17, 18, 19, 20, 21, 22, 23, 24
Text1.Text = "3rd Week"
Case 25, 26, 27, 28, 29
Text1.Text = "4th Week"
Case 30, 31
Text1.Text = "5th Week"
End Select
End Sub

This does not return always true result. Any better code for this ????
Similar Threads
Reputation Points: 11
Solved Threads: 1
Newbie Poster
Ravi Kant is offline Offline
13 posts
since Jan 2009
Mar 4th, 2009
0

Re: How to get Current Week Number of the Month

First off, your code does not take into account if the month starts on a saturday or any other day other than what is considered to be the first day of the week. Second, in a month with 31 days there could actually be 6 weeks in the month depending upon what day the month started and this would depend upon if you are using a fixed week or a dynamic week.

Fixed week means you consider the week to be set from sun to sat or mon to sun while a dynamic week means that the week starts on the first day of the month.

For a dynamic week it is quit simple
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1.  
  2. Dim Remainder As Integer, Week As Integer
  3.  
  4. Remainder = Day("5/15/2009") Mod 7
  5.  
  6. Week = Day("5/15/2009") \ 7
  7.  
  8. If Remainder > 0 Then Week = Week + 1
  9.  
  10. MsgBox Week

However, if you are using fixed weeks then...

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim Remainder As Integer, MyWeek As Integer
  2. Dim DayStart As Integer, MyDateString As String
  3.  
  4. MyDateString = "5/13/2009"
  5.  
  6. DayStart = Weekday(Month(MyDateString) & "/1/" & Year(MyDateString))
  7.  
  8. MyDateString = DateAdd("d", DayStart, MyDateString)
  9.  
  10. Remainder = Day(MyDateString) Mod 7
  11.  
  12. MyWeek = Day(MyDateString) \ 7
  13.  
  14. If Remainder > 0 Then MyWeek = MyWeek + 1
  15.  
  16. MsgBox MyWeek

Good Luck
Reputation Points: 156
Solved Threads: 296
Posting Virtuoso
vb5prgrmr is offline Offline
1,670 posts
since Mar 2009
Mar 4th, 2009
0

Re: How to get Current Week Number of the Month

Thanks vb5prgrmr
But still my problem is not solved. I have DtPicker control in my form and i want the result according to it's value. I try you code giving date as dtpicker value but still problem is same.

Private Sub Command1_Click()

Dim Remainder As Integer, Week As Integer
Remainder = Day(DTPicker1.Value) Mod 7
Week = Day(DTPicker1.Value) \ 7
If Remainder > 0 Then Week = Week + 1
MsgBox Week
End Sub


Your second Code :

Private Sub Command2_Click()

Dim Remainder As Integer, MyWeek As Integer
Dim DayStart As Integer, MyDateString As String
MyDateString = DTPicker1.Value
DayStart = Weekday(Month(MyDateString) & "/1/" & Year(MyDateString))
MyDateString = DateAdd("d", DayStart, MyDateString)
Remainder = Day(MyDateString) Mod 7
MyWeek = Day(MyDateString) \ 7
If Remainder > 0 Then MyWeek = MyWeek + 1
MsgBox MyWeek

End Sub

But both these code does not give always correct result.
Reputation Points: 11
Solved Threads: 1
Newbie Poster
Ravi Kant is offline Offline
13 posts
since Jan 2009
Mar 4th, 2009
0

Re: How to get Current Week Number of the Month

Click to Expand / Collapse  Quote originally posted by Ravi Kant ...
Thanks vb5prgrmr
But still my problem is not solved. I have DtPicker control in my form and i want the result according to it's value. I try you code giving date as dtpicker value but still problem is same.

Private Sub Command1_Click()

Dim Remainder As Integer, Week As Integer
Remainder = Day(DTPicker1.Value) Mod 7
Week = Day(DTPicker1.Value) \ 7
If Remainder > 0 Then Week = Week + 1
MsgBox Week
End Sub


Your second Code :

Private Sub Command2_Click()

Dim Remainder As Integer, MyWeek As Integer
Dim DayStart As Integer, MyDateString As String
MyDateString = DTPicker1.Value
DayStart = Weekday(Month(MyDateString) & "/1/" & Year(MyDateString))
MyDateString = DateAdd("d", DayStart, MyDateString)
Remainder = Day(MyDateString) Mod 7
MyWeek = Day(MyDateString) \ 7
If Remainder > 0 Then MyWeek = MyWeek + 1
MsgBox MyWeek

End Sub

But both these code does not give always correct result.
Yep, the second has an error because of a quick cut and paste. Just remove the "If Remainder" line
Reputation Points: 156
Solved Threads: 296
Posting Virtuoso
vb5prgrmr is offline Offline
1,670 posts
since Mar 2009
Mar 4th, 2009
0

Re: How to get Current Week Number of the Month

You could use the MonthView Control.

The Week Property of that control returns that particular day's week number in relation to the current year.

For example

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim intWeek as integer
  2.  
  3. intWeek = MonthView1.week

intWeek should return the week number for the particular day selected in the MonthView Control.

You'll probably have to extra coding to figure out which week it is in relation to the month, though.
Reputation Points: 49
Solved Threads: 44
Posting Pro in Training
hkdani is offline Offline
426 posts
since Nov 2007
Mar 4th, 2009
0

Re: How to get Current Week Number of the Month

This should work. Might want to clean it up a bit and check the validity and logic of the Select Case Statement. It has some errors. I'll leave that to you to find. But something like the following:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Option Explicit
  2. Dim dFirstDayMonth As Date, intMonth As Integer, strMonthDayOne As String
  3. Dim intFirstSaturday As Integer, intFirstDay As Integer
  4. Dim strDayOne As String, intPickedDay As Integer
  5.  
  6. Private Sub cmdFindWeek_Click()
  7. Dim intPickedDay As Integer
  8. Dim intMonthWeek As Integer
  9. intPickedDay = DTPicker1.Day
  10. intMonth = DTPicker1.Month
  11. strMonthDayOne = CStr(intMonth) & "/1"
  12. dFirstDayMonth = CDate(strMonthDayOne)
  13. intFirstDay = Day(dFirstDayMonth)
  14. ' First Saturday should always be the following:
  15. intFirstSaturday = 8 - intFirstDay
  16. intMonthWeek = FindWeek(intPickedDay)
  17.  
  18. Text1 = "This is week " & CStr(intMonthWeek)
  19. End Sub
  20.  
  21. Private Function FindWeek(CurrentDay As Integer) As Integer
  22. Dim intFirstDayWeekTwo As Integer
  23. intFirstDayWeekTwo = intFirstSaturday + 1
  24. Select Case CurrentDay
  25.  
  26. Case 1 To intFirstSaturday
  27. FindWeek = 1
  28. Case intFirstDayWeekTwo To intFirstDayWeekTwo + 6
  29. FindWeek = 2
  30. Case intFirstDayWeekTwo + 7 To intFirstDayWeekTwo + 12
  31. FindWeek = 3
  32. Case intFirstDayWeekTwo + 14 To intFirstDayWeekTwo + 18
  33. FindWeek = 4
  34. Case intFirstDayWeekTwo + 21 To intFirstDayWeekTwo + 24
  35. FindWeek = 5
  36. Case intFirstDayWeekTwo + 28 To intFirstDayWeekTwo + 30
  37. FindWeek = 6
  38. End Select
  39.  
  40. End Function
Last edited by hkdani; Mar 4th, 2009 at 11:24 am.
Reputation Points: 49
Solved Threads: 44
Posting Pro in Training
hkdani is offline Offline
426 posts
since Nov 2007
Mar 5th, 2009
0

Re: How to get Current Week Number of the Month

Thanks hkdani !
But still it has problem . As for example for 5 january 2009 it is week 2 but it returns week 1 . Please check it.
Reputation Points: 11
Solved Threads: 1
Newbie Poster
Ravi Kant is offline Offline
13 posts
since Jan 2009
Mar 5th, 2009
0

Re: How to get Current Week Number of the Month

Quote ...
But still it has problem . As for example for 5 january 2009 it is week 2 but it returns week 1 . Please check it.
I'm leaving that up to you. I thought it was right, but testing it I did see a few bugs. I think it's a logic error on my part, but then again I also thought maybe the control itself wasn't working right. But I really doubt the control is giving the problem, it's probably me.
Reputation Points: 49
Solved Threads: 44
Posting Pro in Training
hkdani is offline Offline
426 posts
since Nov 2007
Mar 5th, 2009
0

Re: How to get Current Week Number of the Month

Please help ! I am new in VB.
Reputation Points: 11
Solved Threads: 1
Newbie Poster
Ravi Kant is offline Offline
13 posts
since Jan 2009
Mar 5th, 2009
0

Re: How to get Current Week Number of the Month

Okay. The problem is in the Select Case CurrentDay , ..... End Select section.

As someone nicely pointed out in a previous post, you could have a possibility of 6 weeks in a month, if Day 1 fell on a Saturday (maybe even a Friday with 31 days?). That's why there are 6 cases: Case 1, Case 2, Case 3, ...., Case 6.

You simply have to determine that your tests in each of the 6 cases make sense.

Case 1 should always be from 1 to intFirstSaturday.
If you'll look at the code

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. ' First Saturday should always be the following:
  2. intFirstSaturday = 8 - intFirstDay

You should be able to find the actual day for the First Saturday of the month with that formula. The Day() fundtion returns which day of the week a certain day happens to fall on: Sunday, Monday, Tuesday, Wednesday, etc. So, if Day(#3/1/2009#) returns a value of 1, then 8 - 1 = 7: March 7, is Saturday, in other words.

That's the first test for Case 1

Case 2 should start with the Day after the first Saturday or intFirstSaturday + 1 or as in the above code: IntFirstDayWeekTwo.

We're testing from Sunday to Saturday or a period of 7 days. So
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Case (intFirstDayWeekTwo) to (intFirstDayWeekTwo + 6 )

Ah, I think I see the error. It begins in Case 3. I think I should be adding 7 instead of 6. So that should be + 13 instead of 12?, 20 instead of 18, etc.

That may give you enough to go on.
Reputation Points: 49
Solved Threads: 44
Posting Pro in Training
hkdani is offline Offline
426 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Leave Cell msFlexgrid
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Need help with SQL queries





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC