Show date range instead of week number

aldeb

Registered User.
Local time
Today, 18:26
Joined
Dec 23, 2004
Messages
318
When using the Format:'yyyy mm dd' in access queries.
When trying to show a week is there a way to show the
date range instead of week number.

Week 01 = Jan1-7. If the results are only week numbers
that makes it hard for people to understand when it
actually is. Bottom line is: I would like to convert
Week number to actual date range. Can anyone help with
this?
 
The logic is straightforward if week1=1/1/08 -07/01/2008
then start of week dateadd("d",(weeknumber-1)*7,#01/01/2008#) and for the end add 6 more days.
If the Format you are using is not your default you may have to wrap Format rund this and/or maybe your dateadd will be
dateadd("d",(weeknumber-1)*7,#2008 01 01#)
Brian
 
Brian,

I really appreciate your help.

How to I use this in a query to produce the right results. In other words where do I
insert the code?
 
Here is another idea, or the same. Or maybe it's just different... :)
Code:
SELECT [WeekNumberColumnHere],
  dateadd("d", (([WeekNumber] - 1) * 7, #1/1/2008#) AS [Start Date],
    dateadd("d", 7, [Start Date]) AS [End Date]
      ([Start Date] & " - " & [End Date]) AS [Period]

FROM YourTable
Credit goes to Brian (aka Mr. Perfect). :D
 
Everyone:

Below is the current sql of my query. I do not know how to insert or use the code
you have sent. Can you give me an example or some advice?

Code:
SELECT Format([TruckDate],'mmm') AS [Month], Format([TruckDate],'ww') AS Week, Format([TruckDate],'yyyy') AS [Year], Count(*) AS TruckTotals
FROM TruckRegisterTbl
WHERE (((TruckRegisterTbl.Company) Is Not Null) AND ((TruckRegisterTbl.TruckDate) Between [Forms]![VisualInformationForm]![StartDateTxt] And [Forms]![VisualInformationForm]![EndDateTxt]))
GROUP BY Format([TruckDate],'mm'), Format([TruckDate],'mmm'), Format([TruckDate],'ww'), Format([TruckDate],'yyyy')
ORDER BY Format([TruckDate],'mm'), Format([TruckDate],'ww');
 
I'm confused as to what you are trying to do. There is no point in using weeknumber and month in sortings and groupings.
You said in your initial post that you wanted week1 to run from 1st Jan to 7th Jan, this will not happen with Format([datefield],"ww") which will default to a week starting on Sunday, You need
Format([truckdate],"ww",Weekday(DateSerial(Year([truckdate]),1,1)))

I'm guessing that you need something like below

Code:
SELECT  Format([truckdate],"ww",Weekday(DateSerial(Year([truckdate]),1,1))) AS WeekNumber, Format([TruckDate],'yyyy') AS YearNumber, Count(*) AS TruckTotals
FROM TruckRegisterTbl
WHERE (((TruckRegisterTbl.Company) Is Not Null) AND ((TruckRegisterTbl.TruckDate) Between [Forms]![VisualInformationForm]![StartDateTxt] And [Forms]![VisualInformationForm]![EndDateTxt]))
GROUP BY Format([truckdate],"ww",Weekday(DateSerial(Year([truckdate]),1,1)))Format([TruckDate],'yyyy')
ORDER BY Format([TruckDate],'yyyy'), Format([truckdate],"ww",Weekday(DateSerial(Year([truckdate]),1,1)))

This does not give you the weekstart and end dates I'll go get a coffee and think on that. :D

Brian
 
OK

The groupby and order by get a bit convoluted as they represent the WkStartdate but no calculated fields can be present, you may for clarity like to omit those and just collect all data, then in a 2nd query you can simply group and sort on WkStartdate

This is not tested of course but it should point you in the right direction.
BTW Adam you add 6 to weekstartdate to get weekenddate not 7. :D

Brian

Code:
SELECT  Format([truckdate],"ww",Weekday(DateSerial(Year([truckdate]),1,1))) AS WeekNumber, Count(*) AS TruckTotals,   DateAdd("d",([WeekNumber]-1)*7,DateSerial(Year([date of return]),1,1)) as WkStartdate, [Startdate]+ 6 as WkEnddate
FROM TruckRegisterTbl
WHERE (((TruckRegisterTbl.Company) Is Not Null) AND ((TruckRegisterTbl.TruckDate) Between [Forms]![VisualInformationForm]![StartDateTxt] And [Forms]![VisualInformationForm]![EndDateTxt]))
GROUP BY   DateAdd("d",(Format([truckdate],"ww",Weekday(DateSerial(Year([truckdate]),1,1)))-1)*7,DateSerial(Year([truckdate]),1,1))
ORDER BY  DateAdd("d",(Format([truckdate],"ww",Weekday(DateSerial(Year([truckdate]),1,1)))-1)*7,DateSerial(Year([truckdate]),1,1));
 
Week Number to Date Range Function

Hi aldeb:

I've seen a bunch of questions regarding either changing or displaying the date ranges associated with week numbers.


The easiest route I have found to getting what you need is through creating a table. Here is the step by step breakdown.


1. Copy the following function and place it in a module. Then, in the Immediate window (in the code editor, press Ctrl+G), type:
"? CreateWeekTable(*date*,*yrs*)", where

*date* = #1/1/YYYY# format for the time you want to start from, and
*yrs* = the number of years you want week ranges for.

[[so, for 3 years of data in 2009, "? CreateWeekTable(#1/1/2009#, 3)"]]

Code:
Public Function CreateWeekTable(ByVal BeginDate As Date, ByVal Years As Integer)
'***************************************************
'Enter as BeginDate the first day of the year (1/1/YYYY), and a table
'will be made listing the week ranges for the next 4 years.
'***************************************************

    Dim db As DAO.Database
    Dim tbl As TableDef
    Dim xxx As Integer
        xxx = 0 'Create table unless xxx = 1
    Dim varStart As Date
    Dim varFinish As Date
    Dim varWeekNumber As Integer
    Dim varYear As String
    
    
    Set db = CurrentDb
        
        
    '***************************************************
    'Search for existing table, and clear records if available
    '***************************************************
    For Each tbl In db.TableDefs
        If tbl.Name = "tblWeekRanges" Then
            db.Execute "DELETE * FROM tblWeekRanges", dbFailOnError
            xxx = 1 'Do not create table
        End If
    Next tbl
    
    
    '***************************************************
    'If not present, create table.
    '***************************************************
    If xxx = 0 Then
        Dim SQL As String
            SQL = "CREATE TABLE tblWeekRanges (Year TEXT, WeekNo INT, WkStart DATE, WkFinish DATE);"
            DoCmd.SetWarnings (WarningsOff)
            DoCmd.RunSQL SQL
            DoCmd.SetWarnings (WarningsOff)
    End If
    
    
    '***************************************************
    'Set initial values.
    '***************************************************
    'Make BeginDate the beginning of the week
    If DatePart("w", BeginDate) > 1 Then
        varStart = DateAdd("d", -DatePart("w", BeginDate) + 1, BeginDate)
    Else
        varStart = BeginDate
    End If
    
    varFinish = DateAdd("d", 6, varStart)
    varWeekNumber = 1
    
    If DatePart("yyyy", varStart) <> DatePart("yyyy", varFinish) Then
        varYear = DatePart("yyyy", varFinish)
    Else
        varYear = DatePart("yyyy", varStart)
    End If
    
    
    '***************************************************
    'Run first entry
    '***************************************************
    SQL = "INSERT INTO tblWeekRanges (Year, WeekNo, WkStart, WkFinish) VALUES ( " & _
          "'" & varYear & "', '" & varWeekNumber & "', '" & varStart & "', '" & varFinish & "');"
    
    DoCmd.SetWarnings (WarningsOff)
    DoCmd.RunSQL SQL
    DoCmd.SetWarnings (WarningsOff)
    
    
    '***************************************************
    'Enter some period of years...
    '***************************************************
    For yar = 0 To ((Years * 52) - 1) '# of years * 52weeks, minus the first entry above.
        
        varStart = DateAdd("d", 1, varFinish)
        varFinish = DateAdd("d", 6, varStart)
        If varWeekNumber >= 52 Then
            If DatePart("yyyy", varStart) = DatePart("yyyy", varFinish) And DatePart("m", varFinish) <> 1 Then
                varWeekNumber = varWeekNumber + 1
            Else
                varWeekNumber = 1
            End If
        Else
            varWeekNumber = varWeekNumber + 1
        End If
        
        If DatePart("yyyy", varStart) <> DatePart("yyyy", varFinish) Then
            varYear = DatePart("yyyy", varFinish)
        Else
            varYear = DatePart("yyyy", varStart)
        End If
        
        SQL = "INSERT INTO tblWeekRanges (Year, WeekNo, WkStart, WkFinish) VALUES ( " & _
              "'" & varYear & "', '" & varWeekNumber & "', '" & varStart & "', '" & varFinish & "');"
        
        DoCmd.SetWarnings (WarningsOff)
        DoCmd.RunSQL SQL
        DoCmd.SetWarnings (WarningsOff)
        
    Next yar
    
    CreateWeekTable = "Success!"

End Function


2.
In whatever field/fields/event you want the ranges for, you can copy & paste the following code:

Key:
DATE = Whatever field has your MM/DD/YYYY date. The one that is used to generate a week number.


For the first day of the week:
Code:
DLookUp("WkStart","tblWeekRanges","[Year] =  '" & DatePart("yyyy",[I][B]DATE[/B][/I]) & "' AND [WeekNo] = " & DatePart("ww",[I][B]DATE[/B][/I]) & "")
For the last day of the week:
Code:
DLookUp("WkFinish","tblWeekRanges","[Year] =  '" & DatePart("yyyy",[I][B]DATE[/B][/I]) & "' AND [WeekNo] = " & DatePart("ww",[I][B]DATE[/B][/I]) & "")
You can use the two in various ways to create strings like [1/1/2009 -- 1/7/2009], or whatever you need to do.



*********Also, here are the code snippets if you just have week-numbers.****************



3.
In whatever field/fields/event you want the ranges for, you can copy & paste the following code:

Key:
YEAR = The year your week-number is in, or the field that contains that information.
WEEK# = The week-number itself, or the field that contains it.


For the first day of the week:
Code:
DLookUp("WkStart","tblWeekRanges","[Year] = '[I][B]YEAR[/B][/I]' AND [WeekNo] = [I][B]WEEK#[/B][/I]")
For the last day of the week:
Code:
DLookUp("WkFinish","tblWeekRanges","[Year] = '[I][B]YEAR[/B][/I]' AND [WeekNo] = [I][B]WEEK#[/B][/I]")
If you ever need to expand the table, you just need to run it again @ the immediate window, or call it in your code. The function will clear the previous data and refill it with your new parameters.

I hope this makes it a bit simpler.

-V
 
Last edited:
Thanks Vigalante,

I will give this a try.
 

Users who are viewing this thread

Back
Top Bottom