Calculating Holiday Entitlement

JohnLee

Registered User.
Local time
Today, 08:24
Joined
Mar 8, 2007
Messages
692
Good afternoon folks,

I hoping someone may be able to offer some assistance in improving the code I have written to calculate holiday entitlement. The code works fine, it's just that I'm not sure it's the most efficient way of writing it, so any one could spear a moment to look over my code and let me have their view and or suggestions it would be most appreciated.

The code is placed in the on current event of my form.

I'm the most proficient in code writing, so please bear with me if I come back to you with clarification of any suggestions. thanks in advance the code is below:

===============
START CODE
===============
Const HolEntMon As Double = 1.83 'Declares the HolEntMon variable as a constant value of 1.83
Const HolHours44 As Double = 4.4 'Declares the HolHours44 as a constant value of 4.4
Const WorkDay As Integer = 5 'Declares the WorkDay variable as a constant value of 5
Const MonthsPerYear As Integer = 12 'Declares the MonthsPerYear variable as a constant value of 12
Const AnnualHoldays As Integer = 22 'Declares the AnnualHolidays variable as a constant value of 22

'Calculates the individuals Holiday entitlement in days according to the number of days worked
'per week.

'If the individual works 7 days a week then calculate the HolEntMon [1.83]/Workday [5] *
'the number of days worked per week [txtNumberOfWeeklyWorkDays]* MonthsPeryear [12]and
'assign the result to the [txtHolsEntDays] text box.
If Me![txtNumberOfWeeklyWorkDays] = 7 Then
Me![txtHolsEntDays] = HolEntMon / WorkDay * Me![txtNumberOfWeeklyWorkDays] * MonthsPerYear
'If the individual works 6 days a week then calculate the HolEntMon [1.83]/Workday [5] *
'the number of days worked per week [txtNumberOfWeeklyWorkDays]* MonthsPeryear [12]and
'assign the result to the [txtHolsEntDays] text box.
ElseIf Me![txtNumberOfWeeklyWorkDays] = 6 Then
Me![txtHolsEntDays] = HolEntMon / WorkDay * Me![txtNumberOfWeeklyWorkDays] * MonthsPerYear
'If the individual works 5 days a week then calculate the HolEntMon [1.83]/Workday [5] *
'the number of days worked per week [txtNumberOfWeeklyWorkDays]* MonthsPeryear [12]and
'assign the result to the [txtHolsEntDays] text box.
ElseIf Me![txtNumberOfWeeklyWorkDays] = 5 Then
Me![txtHolsEntDays] = HolEntMon / WorkDay * Me![txtNumberOfWeeklyWorkDays] * MonthsPerYear
'If the individual works 4 days a week then calculate the HolEntMon [1.83]/Workday [5] *
'the number of days worked per week [txtNumberOfWeeklyWorkDays]* MonthsPeryear [12]and
'assign the result to the [txtHolsEntDays] text box.ElseIf Me![txtNumberOfWeeklyWorkDays] = 4 Then
ElseIf Me![txtNumberOfWeeklyWorkDays] = 4 Then
Me![txtHolsEntDays] = HolEntMon / WorkDay * Me![txtNumberOfWeeklyWorkDays] * MonthsPerYear
'If the individual works 3 days a week then calculate the HolEntMon [1.83]/Workday [5] *
'the number of days worked per week [txtNumberOfWeeklyWorkDays]* MonthsPeryear [12]and
'assign the result to the [txtHolsEntDays] text box.
ElseIf Me![txtNumberOfWeeklyWorkDays] = 3 Then
Me![txtHolsEntDays] = HolEntMon / WorkDay * Me![txtNumberOfWeeklyWorkDays] * MonthsPerYear
'If the individual works 2 days a week then calculate the HolEntMon [1.83]/Workday [5] *
'the number of days worked per week [txtNumberOfWeeklyWorkDays]* MonthsPeryear [12]and
'assign the result to the [txtHolsEntDays] text box.
ElseIf Me![txtNumberOfWeeklyWorkDays] = 2 Then
Me![txtHolsEntDays] = HolEntMon / WorkDay * Me![txtNumberOfWeeklyWorkDays] * MonthsPerYear
'If the individual works 1 day a week then calculate the HolEntMon [1.83]/Workday [5] *
'the number of days worked per week [txtNumberOfWeeklyWorkDays]* MonthsPeryear [12]and
'assign the result to the [txtHolsEntDays] text box.
ElseIf Me![txtNumberOfWeeklyWorkDays] = 1 Then
Me![txtHolsEntDays] = HolEntMon / WorkDay * Me![txtNumberOfWeeklyWorkDays] * MonthsPerYear
End If

===============
END CODE
===============

cheers

John
 
Look into using the SELECT CASE instead of IF and ElseIF:

Code:
Select Case Me![txtNumberOfWeeklyWorkDays] 
   Case 1
      Do your stuff here
   Case 2
      Do whatever for 2 here
End Select
that was just a very short example, but hopefully you get the idea.
 
This code:
Code:
  [FONT=&quot]Me![txtHolsEntDays] = HolEntMon / WorkDay * Me![txtNumberOfWeeklyWorkDays] * MonthsPerYear[/FONT]
is the same in each case - why duplicate it. Also, your constants are not likely to ever change (5 workdays and 12 months in a year) so hard coding them won't hurt and will save code. Simplify it even more by doing the multiplication (5 * 12 = 60) and enclose the right half in parenthesis to ensure the multiplication is done before the division. So your much shorter code would look like this:
Code:
    [COLOR=blue]Const[/COLOR] HolEntMon [COLOR=blue]As Double[/COLOR] = 1.83 [COLOR=green]'Declares the HolEntMon variable as a constant value of 1.83[/COLOR]
[COLOR=blue]Const[/COLOR] HolHours44 [COLOR=blue]As Double[/COLOR] = 4.4 [COLOR=green]'Declares the HolHours44 as a constant value of 4.4[/COLOR]
[COLOR=blue]Const[/COLOR] AnnualHoldays [COLOR=blue]As Integer[/COLOR] = 22 [COLOR=green]'Declares the AnnualHolidays variable as a constant value of 22[/COLOR]
 
 
  [FONT=&quot]
Me![txtHolsEntDays] = HolEntMon / (60 * Me![txtNumberOfWeeklyWorkDays)
 
 [/FONT]
 
Hi Guys,

Thanks very much for both of your suggestions, I will amend my code accordingly.

John
 

Users who are viewing this thread

Back
Top Bottom