Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Accounting Year Week Calculations

Introduction.

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.


Function: AccWeek().

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
'All Rights Reserved by www.msaccesstips.com
'--------------------------------------------------------------
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:
Share:

1 comment:

Comments are subject to moderation before publishing.

Translate



PageRank
Subscribe in a reader
Your email address:

Delivered by FeedBurner

http://clicky.com/101199826
Blog Directory Visit blogadda.com to discover Indian blogs

Search

Popular Posts

Search This Blog

Blog Archive

Powered by Blogger.

Follow by Email

Labels

Forms Functions How Tos MS-Access Security Reports Class Module msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Array Queries msaccess reports External Links msaccess tips Accesstips Downloads Objects Property Collection Object Event Menus and Toolbars Controls MsaccessLinks Process Controls WithEvents Art Work msaccess How Tos Combo Boxes Graph Charts List Boxes VBA msaccessQuery Command Buttons Dictionary Object Form Report Calculation Command Button Data Emails and Alerts Query RaiseEvent Custom Functions Custom Wizards DOS Commands Data Type Object Reference ms-access functions msaccess functions msaccess graphs msaccess reporttricks Item msaccessprocess security advanced Access Security Add Auto-Number Field Type Fields Form Instances Key Macros Menus SubForm Top Values Variables msaccess email msaccess menus progressmeter Access2007 Copy Excel Expression Join Methods Microsoft Numbering System Records Recordset Security Split Table Time Difference Utility Workgroup Wrapper Classes database function ms-access msaccess wizards reference text tutorial vba code Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Conditional Formatting Data Filtering Defining Pages Diagram Disk Dynamic Lookup Error Handler Export External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Tables Union Query User Users Water-Mark Word automatically commands hyperlinks iSeries Date iif msaccess msaccess alerts pdf files restore switch toolbar updating upload

Featured Post

Running Sum in MS-Access Query

Introduction. We are familiar with creating Auto-Numbers in Query Column, through an earlier Function created with the Name QrySeq(), publi...

Labels

Blog Archive

Recent Posts