Solved

print a weeknumber in a textfield

Posted on 2000-03-09
15
319 Views
Last Modified: 2012-05-04
How I'll able to print the current weeknumber on an Visual Basic application.

Example:
when teh date is 3 januari
the application must print week 1
in a textfield

Thx in advance
Der Freak
0
Comment
Question by:FREAK
  • 6
  • 5
  • 3
  • +1
15 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 2601328
Private Sub Command1_Click()
    Dim d As Date
    Dim nWeek As Long
    d = Now
    nWeek = DatePart("ww", d)
    MsgBox "Week " & CStr(nWeek)
End Sub
0
 

Author Comment

by:FREAK
ID: 2601360
Thx...but I think the code is not 100%

I test it but he said that it's week 11
and it is week 10 now

???
0
 
LVL 32

Expert Comment

by:bhess1
ID: 2601411
It depends on which standard you are using for week number.  In some locales, week numbers start on the first week of a year with more than 3 days in it (for accounting purposes, at least - this is, as I understand it, used primarily in some European countries.)

The simple answer has already been given.  A more complex answer to andle the above case would be something like this:

Function WeekNumber (ByVal Dt as Date) As Integer

Dim Week%, tmpDt
Dim YrDay1%, WeekOfs%

  tmpDt = CVDate("01/01/" & Format(Dt,"yyyy"))  ' Get new years day
  YrDay1 = DatePart("w", tmpDt) ' What day of week was New Years for the input date?

  WeekOfs = (YrDay1 > 4) ' If 3 or less days in 1st week, WeekOfs = -1

  Week = DatePart("ww", Dt) + WeekOfs
  If Week = 0 Then Week = Weeknumber(tmpDt - 1)
  WeekNumber = Week

End Function

This should give the correct week number for the European case.  Note:  Yes, the week number for Jan 1 - 3 can be week 53!
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 4

Accepted Solution

by:
wesleystewart earned 50 total points
ID: 2601413
Erick37 supplied the correct function, but the optional third parameter must be used to determine which week is the first week in the year.

Depending on your locale, the first week of the year might be the first week in which 1/1 appears, or it might be the first week in which 1/4 appears, or it might be the first full week of the year.

I think for you the correct syntax would be:

Private Sub Command1_Click()
    Dim d As Date
    Dim nWeek As Long
    d = Now
    nWeek = DatePart("ww", d, 1)
    MsgBox "Week " & CStr(nWeek)
End Sub

to specify other "first weeks" use the following constants:

1 first week contains 1/1
2 first week has at least the first four days of the year
3 first week is first full week.

Hope this helps.

Wes
0
 
LVL 32

Expert Comment

by:bhess1
ID: 2601430
One more possibility:  If you are simply counting the number of seven day periods since Jan 1, then use:

Function WeekNumber(Dt as Date) As Integer

Dim tmpDt, Days%

  tmpDt = CVDate("01/01/" & Format(Dt, "yyyy")
  Days = DateDiff("d", tmpDt, Dt)
  WeekNumber = Days Mod 7 + 1
End Function

0
 
LVL 32

Expert Comment

by:bhess1
ID: 2601452
Well, poop - I hadn't ever read that part of the DatePart documentation!  Thanks for the information (we can all learn something new here, even if we think we know the answer).
0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2601455
Happy to help.

Note that the comment from bhess1 would certainly work, and if you want total control over the return that is probably the way to go.  But using DatePart() allows you to take advantage of built-in (fast!!) functions and constants.

Wes
0
 

Author Comment

by:FREAK
ID: 2601487
I think this is working

Private Sub Command1_Click()
    Dim d As Date
    Dim weekNummer As Long
    d = Now
    weekNummer = DatePart("ww", d)
    MsgBox ("Week " & (weekNummer - 1))
End Sub
0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2601518
That won't work for a few days out of every week.  You might find out tomorrow that you are getting answers that indicate the previous week.  You really need to use that third argument:

weekNummer = DatePart("ww", d, 1)

Wes
0
 

Author Comment

by:FREAK
ID: 2601543
Private Sub Command1_Click()
    Dim d As Date
    Dim nWeek As Long
    d = now
    nWeek = DatePart("ww", d, 1)
    MsgBox "Week " & CStr(nWeek)
End Sub

gives me as answer week 11
and it's week 10 at the moment
0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2601567
My calendar says it is week 11 at the time.  How are you calculating the first week in the year?

1/1/2000 was a Saturday, so the first week of the year was exactly one day long.  If you are under the impression that the first week of the year is the first full week, then use the constant 3:

=DatePart("ww", d, 3)

Wes
0
 

Author Comment

by:FREAK
ID: 2601569
Private Sub Command1_Click()
    Dim d As Date
    Dim nWeek As Long
    d = now
    nWeek = DatePart("ww", d, 1)
    MsgBox "Week " & CStr(nWeek)
End Sub

gives me as answer week 11
and it's week 10 at the moment
0
 
LVL 4

Expert Comment

by:wesleystewart
ID: 2601574
Crap.

I was wrong.  The firstweek argument is the FOURTH argument.  You have to include a comma for the option third argument, which we won't include:

datepart("ww",date(),,3)

Will return week 10

datepart("ww",date(),,1)

returns week 11

Sorry about the mix-up

Wes
0
 

Author Comment

by:FREAK
ID: 2601589
At my calender is sunday 2 januari 2000 the first day of week 1. I live in Holland....maybe is that the difference

=datePart("ww",d,3)

constant 3 gives also week 11 as answer.
grtz..
FREAK
0
 

Author Comment

by:FREAK
ID: 2601596
Thank u very much Wesley...
This code is working 100% !!!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Macro: print each sheet to pdf using ExportAsFixedFormat 11 48
Protecting vb6 & .Net code Obfuscation 18 124
Excel Automation VBA 19 71
RUNRMTCMD from AS/400 12 47
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question