Solved

How do I populate a numeric field with a Week Number

Posted on 2015-02-02
5
128 Views
Last Modified: 2015-02-02
I have a labor record for employees where we record the hours worked associated with a week number. The week number is formatted as a numeric field.

What I am looking to do is have an unbound control where the date is entered, (like week ending date), have it return "ww" week number format and it becomes the default input for the records that follow.

I am a novice and do not know how to turn a date format result into a number format in another field.

unbound field in header = [weekEnding]  ; basically date() ww
bound field = [tblEmployeeHours_WeekNo] ; format number

mij
0
Comment
Question by:mijwil
5 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 40584456
you can use the datepart() function

datepart( "ww",Date())
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 40584465
Rather than storing the week number, store the week ending date.  You can always derive the week as you need it.  Add an unbound control to the main form.  Change the Name property to txtWeekEndingDate.  Set its format to short date so

In the BeforeInsert event of the subform  where you are entering the details.

Me.WeekEndingDate = Me.Parent!txtWeekEndingDate

Then when you need the week number, use the DatePart() function to get it.

Select ..., datepart("ww", WeekEndingDate) As WeekNum
From YourTable;
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40584529
no points please.  I tend to agree with Pat on this one.  I prefer to store a date, from which I can generate year, month, day, weekday, week, ...

If you store the week #, you will also need to include a field in your table for year.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40584633
Store the date and later retrieve the year-week. This is, however, not as easy as you may think as Access always has been buggy calculating true week numbers.

This function does it right:
Public Function ISO_WeekYearNumber( _
  ByVal datDate As Date, _
  Optional ByRef intYear As Integer, _
  Optional ByRef bytWeek As Byte) _
  As String

' Calculates and returns year and week number for date datDate according to the ISO 8601:1988 standard.
' Optionally returns numeric year and week.
' 1998-2007, Gustav Brock, Cactus Data ApS, CPH.
' May be freely used and distributed.

  Const cbytFirstWeekOfAnyYear  As Byte = 1
  Const cbytLastWeekOfLeapYear  As Byte = 53
  Const cbytMonthJanuary        As Byte = 1
  Const cbytMonthDecember       As Byte = 12
  Const cstrSeparatorYearWeek   As String = "W"
  
  Dim bytMonth                  As Byte
  Dim bytISOThursday            As Byte
  Dim datLastDayOfYear          As Date

  intYear = Year(datDate)
  bytMonth = Month(datDate)
  bytWeek = DatePart("ww", datDate, vbMonday, vbFirstFourDays)
  
  If bytWeek = cbytLastWeekOfLeapYear Then
    bytISOThursday = Weekday(vbThursday, vbMonday)
    datLastDayOfYear = DateSerial(intYear, cbytMonthDecember, 31)
    If Weekday(datLastDayOfYear, vbMonday) >= bytISOThursday Then
      ' OK, week count of 53 is caused by leap year.
    Else
      ' Correct for Access97/2000+ bug.
      bytWeek = cbytFirstWeekOfAnyYear
    End If
  End If

  ' Adjust year where week number belongs to next or previous year.
  If bytMonth = cbytMonthJanuary Then
    If bytWeek >= cbytLastWeekOfLeapYear - 1 Then
      ' This is an early date of January belonging to the last week of the previous year.
      intYear = intYear - 1
    End If
  ElseIf bytMonth = cbytMonthDecember Then
    If bytWeek = cbytFirstWeekOfAnyYear Then
      ' This is a late date of December belonging to the first week of the next year.
      intYear = intYear + 1
    End If
  End If
  
  ISO_WeekYearNumber = CStr(intYear) & cstrSeparatorYearWeek & Format(bytWeek, "00")

End Function

Open in new window

/gustav
0
 

Author Closing Comment

by:mijwil
ID: 40584744
Perfect. Thanks
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

864 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

24 Experts available now in Live!

Get 1:1 Help Now