Solved

How do I populate a numeric field with a Week Number

Posted on 2015-02-02
5
127 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
Comment Utility
you can use the datepart() function

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

Expert Comment

by:PatHartman
Comment Utility
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)
Comment Utility
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
Comment Utility
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
Comment Utility
Perfect. Thanks
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

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 previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

772 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

11 Experts available now in Live!

Get 1:1 Help Now