Repost - How to code (shift rotation calendar function)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need code to build a profile based on a date that is tied to a 'platoon'
or roster.

Let's say the day is March 6, 2006. Today (feb 28) is the day I want the
code to use as a 'base' . I have 5 platoons, working 4 days on and six days
off. Each platoon works two days then two nights.

Today is platoon 'c' second day. That means tomorrow is platoon 'd' first
day and platoon 'c' first night. So using this, March 6, 2006 is platoon 'a'
second day and platoon 'b' second night.

Anyone done anything like this. I don't even know where to begin!

Cheers
 
I have some thoughts that might prompt someone to help fill in the blanks.

The rotation is a 10 day cycle. So if I could figure out how many days
between the seed date (for example Feb 01/06) and the date needed (Feb
21/06), which is 21 days, then only look at the '1' digit, in this case ONE.
I can then match this digit to the table to determine which platoon is on
days and which one is on nights.

Any ideas???


Thanks!
 
Paul, If I understand what you mean, then if feb. 28 is your first day, then
March 6 is the 7th day, not the 6th, therefore it should be the first day of
d platoon, because the second day of platoon 'c' was March 6th. I mean, if
you check only the day platoons it should look like that:
28 (feb) - a
1 (march) - a
2 - b
3 - b
4 - c
5 - c
6 - d (That's where you're saying it's platoon 'c' second day)

Am I correct?
 
Hi,

I've written a code to do what I understand from your words. Tell me if it
does what you want. In order to run that code you need a form with 2 textbox
controls: the first textbox is called 'txtSeedDate' and the second textbox
is called 'txtTargetDate'. In addition, you need to have a command button
called 'cmdCalculate' on that form.

After you set the dates into the 2 textboxes, you should click the
cmdCalculate command button, and see the result according to the dates
entered. For example of you enter '28/02/2006' as the seed date and
'06/03/2006' as the target date, you'll get the following result:
Day #1 of platoon D
Night #1 of platoon C

After you understand the logic, you might change the code to suit your
needs.

As I posted in my previous reply, there might be a chance that I havn't
understood you perfectly, so you might clarify your demands or just fix the
code by yourself.

In order for the code to work, create the form with the 2 textboxes and
command button I've mentioned, then copy the following text into the form's
code:
'*********************Code Begin*****************************
Option Explicit

Private Sub cmdCalculate_Click()

'Variables declaration
Dim intDifferenceInDays As Integer
Dim intDaysInOneCycle As Integer
Dim intDifferenceInDaysOfTheCycle As Integer
Dim intTotalNumberOfPlatoons As Integer
Dim intDaysBetweenPlatoonSwitches As Integer

'Variables value settings (You might change values)
intDaysInOneCycle = 10
intTotalNumberOfPlatoons = 5
'Following line means that each platoon is working
'2 days then 2 nights, so that each platoon
'is replaced cylicaly every 2 days.
intDaysBetweenPlatoonSwitches = 2

'Input Validation
If IsNull(Me.txtSeedDate) Or IsNull(Me.txtTargetDate) Then
MsgBox "Missing date"
Exit Sub
End If

'Calculate difference in days between 2 dates entered
'"d" is used in order for the difference
'to be calculated in units of days
intDifferenceInDays = DateDiff("d", _
Me.txtSeedDate, Me.txtTargetDate)

'Calculates the difference in days according
'to the cycle. e.g. the difference between
'1/1/2006 and 13/1/2006 is 2 since cycle is 10 days
intDifferenceInDaysOfTheCycle = _
(intDifferenceInDays Mod intDaysInOneCycle)

'Show who works on the target day
MsgBox "Day " & _
DayPlatoonAccordingToDifference(intDifferenceInDaysOfTheCycle, _
intDaysInOneCycle) & _
vbCrLf & _
"Night " & _
NightPlatoonAccordingToDifference(intDifferenceInDaysOfTheCycle, _
intDaysInOneCycle)

End Sub


'************************************************************
'Function Name: DayPlatoonAccordingToDifference
'Purpose: Returns the letter (a, b, etc.) of the platoon
' who works on the day which is in a given
' difference from the seed day.
'Example: 0 and 1 will give: "A", 2 and 3 will give "B" etc.
'Input variables:
' intDifferenceInDaysOfCycle - The difference in "cyclic"
' days between the seed date and the target date
' intDaysInOneCycle - Amount of days in one cycle
'************************************************************
Function DayPlatoonAccordingToDifference(intDifferenceInDaysOfCycle _
As Integer, intDaysInOneCycle As Integer) As String

Dim intACharASCIIValue As Integer
Dim intNormalizedDifferenceInDays As Integer
intACharASCIIValue = 65

intNormalizedDifferenceInDays = _
NormalizedDifferenceInDays(intDifferenceInDaysOfCycle, _
intDaysInOneCycle)

Select Case (intNormalizedDifferenceInDays Mod 2)
Case 0
DayPlatoonAccordingToDifference = _
"#1 of platoon "
Case 1
DayPlatoonAccordingToDifference = _
"#2 of platoon "
End Select

DayPlatoonAccordingToDifference = _
DayPlatoonAccordingToDifference & _
Chr(intACharASCIIValue _
+ ((intNormalizedDifferenceInDays - _
(intNormalizedDifferenceInDays Mod 2)) / 2))

End Function


'************************************************************
'Function Name: NightPlatoonAccordingToDifference
'Purpose: Returns the letter (a, b, etc.) of the platoon
' who works on the night which is in a given
' difference from the seed day.
'Example: 0 and 1 will give: "E", 2 and 3 will give "A" etc.
'Input variables:
' intDifferenceInDaysOfCycle - The difference in "cyclic"
' days between the seed date and the target date
' intDaysInOneCycle - Amount of days in one cycle
'************************************************************
Function NightPlatoonAccordingToDifference(intDifferenceInDaysOfCycle _
As Integer, intDaysInOneCycle As Integer) As String

NightPlatoonAccordingToDifference = _
DayPlatoonAccordingToDifference(intDifferenceInDaysOfCycle _
- 2, intDaysInOneCycle)

End Function


'************************************************************
'Function Name: NormalizedDifferenceInDays
'Purpose: For positive input values returns original number.
' For negative input returns original number+cycle.
'Example: +5 remains +5. -2 becomes +8.
'Input variables:
' intOriginalDifference - The difference in "cyclic"
' days between the seed date and the target date
' intDaysInOneCycle - Amount of days in one cycle
'************************************************************
Function NormalizedDifferenceInDays(intOriginalDifference _
As Integer, intDaysInOneCycle As Integer) As Integer

If intOriginalDifference < 0 Then
NormalizedDifferenceInDays = intOriginalDifference _
+ intDaysInOneCycle
Else
NormalizedDifferenceInDays = intOriginalDifference
End If

End Function
'*********************Code End*******************************

There also might be clever ways of doing what I've written. It's 06:30 in
the morning where I live... :-)

Hope it helps.

Regards,
Amir.
 
Wow....Thanks Amir. I haven't actually tried your code, but had a quick look
through it. I will let you know how it works out.

Thanks again.
 
Hi Amir,
I've modified you code to suit my application (only 4 crews) and it works
nicely.

The only problem I'm having, is that shift change is at 7:00am and 7:00pm.
But I need to know what crew is on when a specific event occurs. So what
happens is when I punch in a date and it is the first day shift of Crew A
(for example), then your program tells me for April 5th, 2007 that A crew is
on. But if the event I'm looking at occurs between midnight and 7:00am, then
it is on a different crew (the second shift of the night crew).

I don't know how to modify the program to compensate for this. Would you
shift the date change by 7hours or would you write an IF statement in case
the event occurs between 00:00 and 07:00 THEN.....

Thanks in advance for your help and thanks for the code you posted. The
whole time I was going through it, I was thinking "I'm not worthy!,... I'm
not worthy! :) )



Pete
 
Back
Top