Let us get straight into an issue in computerized calculations.  Most companies maintain their Books of Accounts for the Accounting Year starting from April 1st and ending on March 31st, next year.  So, April is the first month of the Accounting Year and 12th month is March next year.

Here, our focus is on the Accounting Weeks calculations. If we need Weekly analysis of Sales or some other activities of the company how do we calculate week numbers starting from April 1-7 as Week 1 instead of January 1-7?  January 1 may sometimes end up as week 53 or 54, overlapping to next year based on the start of the week day.

We have the built-in Function DatePart() in Microsoft Access to calculate Week numbers based on the activity-date provided to the function as parameter.  Let us try an example of Datepart() function directly on the Debug Window.

Date is in dd-mm-yyyy format and Sunday is taken as first day of the week. The VBA Constant vbSunday represents the numeric value 1.

```dt = DateValue("01-04-2011")
? DatePart("ww",dt,vbSunday)

Result: 14```

The DatePart() built-in function returns the week number 14 instead of 1, for accounting-week calculation period April 1-7. This may fluctuate between 13 and 14, based on the first day of the Week being Sunday (used as second parameter to the above function) and the year. You will get the result 13 for April 1st, 2006.

If we want to create a weekly Sales Graph Chart for the first Quarter (first 13 weeks during April, May and June) of the Accounting Year we must convert the Sales date into their corresponding Week numbers in order to summarize the Sales values into weekly totals.

In short, the DatePart() function cannot be used directly to calculate Accounting months, weeks without some modifications. We can use the DatePart() function within Code of our own to modify the result to get the output we want.

I have written such a function AccWeek() to calculate week numbers from 01-04-yyyy to 31-03-yyyy.  This function mainly intended to call from the query column with activity-date as parameter (like Sale date, Payment date and so on) to return itâ€™s corresponding week number.  You can use the function in your Code, on Form or Report or anywhere else you need it.

Copy and paste the following Code into a Standard Module in your Database and save it:

```Public Function AccWeek(ByVal accDate As Date) As Integer
'--------------------------------------------------------------
'Author : a.p.r.pillai
'Date   : June 2012
'--------------------------------------------------------------
Dim wkdayStart As Integer, wk As Integer, wkout As Integer
Dim accStart As Date, wksave As Integer, accStart1, accStart2

On Error GoTo AccWeek_Err
'First day of the week is taken Sunday as default
'If change is needed in your area please change the next line
wkdayStart = vbSunday
'calculate week number with built-in function
wk = DatePart("ww", accDate, wkdayStart)
'modify the week number according to accounting period
wksave = IIf(wk = 13 And ((Year(accDate) - 1) Mod 4) = 0, wk + 1, 13)
Select Case wk
Case Is <= 13, 14
wkout = DatePart("ww", DateValue("31-12-" & Year(accDate)), vbSunday) - wksave + wk
Case Is > wksave
wkout = wk - wksave
End Select

accStart1 = "01-04-" & Year(accDate)
accStart2 = "08-04-" & Year(accDate)

'Overlapped Week days check and reset week to 1
If (accDate >= accStart1) And (accDate < accStart2) Then
wk = DatePart("ww", accDate, vbSunday)
If wk > 1 Then
wkout = 1
End If
End If
AccWeek = wkout

AccWeek_Exit:
Exit Function

AccWeek_Err
MsgBox Err.Description, , "AccWeek()"
Resume AccWeek

End Function```

Usage Example-1: Calling AccWeek() Function from a Query Column:

SaleWeek:AccWeek([SaleDate])

Usage Example-2: Use it in a Text Box on a Form or Report:

=AccWeek([SaleDate])

Usage Example-3: Call from within your own Code:

intSaleWeek = AccWeek(dtSaleDate)

Note:AccWeek() Function is not extensively tested in field conditions and may use it at your own risk. If you find any logical errors in the code please share it with me too.

Technorati Tags:

Key Words: Function, Accounting,Weeks,Graph Chart