Solved

VB6 = Week of the year in TextBox

Posted on 2012-12-30
9
479 Views
Last Modified: 2013-01-06
Hello all

I'm trying to find a way to put in Text1 the week number if the year.

Ex:
Between Dec 30 2012 and January 5ft 2013, this =  Week 1
Between Jan 6 2012 and January 12 2013, this =  Week 2....


So when i load the form with my Text1, today, i would see 1 (Being week 1)

How can i do that.

Thanks again for your help
0
Comment
Question by:Wilder1626
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
ID: 38731270
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38731274
I think i fount it.but i just need to tweak it cause i have week 53 on today's date but when i look at my company calendar, it count today as week 1 for 2013


 Dim iNumberOfTheWeek As Integer
    iNumberOfTheWeek = DatePart("ww", Now())
    MsgBox iNumberOfTheWeek

Open in new window

0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38731281
But if i do this, now i have week 1:

        Dim iNumberOfTheWeek As Integer
    iNumberOfTheWeek = DatePart("ww", Now() + 2)
    MsgBox iNumberOfTheWeek

Open in new window


weird.
0
 
LVL 29

Accepted Solution

by:
IrogSinta earned 250 total points
ID: 38731301
If you count the weeks from the beginning of the year, today is really the start of the 53rd week.  However, if you base it on a 52 week year, today is really Week 1 (the 53rd week and the 1st week overlap at times).  

So to tweak it, you need to add a conditional statement to return a "1" if the week is "53"
iNumberOfTheWeek = IIf(DatePart("ww", Now())=53, 1, DatePart("ww", Now()))
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 29

Expert Comment

by:IrogSinta
ID: 38731304
You don't want to add 2 to Now() because you are looking up the week number for 2 days later which is Jan 1st.
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 38731348
Hi again.

Thank you so much both of you, again
all work great now.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 38731404
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2012
0
 
LVL 15

Expert Comment

by:eemit
ID: 38731540
Here is another, possibly the right way to obtain Week Of the Year

  'Syntax for DatePart:
  'DatePart (Interval, Date,  [FirstDayOfWeek, [FirstWeekOfYear]] )
  'The FirstWeekOfYear argument can have one of the following settings.
  '2 - Week that has at least four days in the new year (complies with ISO standard 8601, section 3.17)
  '1 - Week in which January 1 occurs (default)
  '0 - First week of year specified in system settings

  'iNumberOfTheWeek = DatePart("ww", Now(), vbMonday, 2)
  'MsgBox iNumberOfTheWeek

  'You can use GetLocaleInfo API to obtain FirstWeekOfYear Specifier
  iNumberOfTheWeek = DatePart("ww", Now(), vbMonday, GetMyLocaleInfo(LOCALE_IFIRSTWEEKOFYEAR))
  MsgBox iNumberOfTheWeek


Option Explicit
   
Private Declare Function GetLocaleInfo Lib "kernel32" _
   Alias "GetLocaleInfoA" (ByVal Locale As Long, _
   ByVal LCType As Long, ByVal lpLCData As String, _
   ByVal cchData As Long) As Long

Private Declare Function GetUserDefaultLCID Lib "kernel32" () As Long
Private Const LOCALE_IFIRSTWEEKOFYEAR As Long = &H100D  'first week of year specifier

Private Function GetMyLocaleInfo( _
                    ByVal LCType As Long _
                    ) As String
                    
  Dim sReturn As String
  Dim iRet1 As Long
  Dim iRet2 As Long
  Dim lpLCDataVar As String
  Dim Pos As Integer
  Dim Locale As Long
   
  Locale = GetUserDefaultLCID()
  iRet1 = GetLocaleInfo(Locale, LCType, lpLCDataVar, 0)
   
  sReturn = String$(iRet1, 0)
  iRet2 = GetLocaleInfo(Locale, LCType, sReturn, iRet1)
  Pos = InStr(sReturn, Chr$(0))
  If Pos > 0 Then
     sReturn = Left$(sReturn, Pos - 1)
  End If
  
  GetMyLocaleInfo = sReturn
  
  Exit Function

End Function

Open in new window

0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38731694
Thanks eemit

This is also very good to know
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now