E
Evi
I wanted to find out which factory shift, a particular time (Now()) falls
into
TblShift has
ShiftName,-ShiftStart(time), ShiftEnd(Time)
Shift1- 09:00:00 -14:00:00
Shift2 - 14:00:01 - 16:00:00
Shift3 - 18:00:01 - 04:00:00
So if Now() = 14:30, I want the result to be "Shift2"
because 14:30 is between 14:00: and 16:00
If Now() = 08:00, I want it to read "No Shift"
Dlookup between ShiftStart and ShiftEnd worked until Midnight struck, then I
got a "No Shift" because Now() at 01:00 was no longer greater than or equal
to ShiftStart and less than or equal To ShiftEnd - it was less than both
these values.
I couldn't figure out an IIF to sift this out.
To make this work, I've added a dummy date to the shift times 25/12/2008
with the Over-midnight shiftEnd being 26/12/08
I've formatted them as General Date
This function does the trick but was there an easier way without using
Recordset (which doesn't always translate easily over different versions of
Access)?
(I've left the dtFind variant there so that the function can be used say to
look up a worker's shift by using the time he started work)
Function ShiftFind(ByVal dtFind As Variant) As String
Dim dtNow As Date
Dim Dummydate As Date
Dim MyFindField As String
Dim MyStartField As String
Dim MyEndField As String
Dim MyTable As String
Dim MyStart As Date
Dim MyEnd As Date
Dim MyFind As String
Dim rs As Recordset
Dim a As Integer
If Not IsNull(dtFind) Then
'change these names to match your Table
MyStartField = "ShiftStart"
MyEndField = "ShiftEnd"
MyFindField = "ShiftName"
MyTable = "QryShiftTest"
'Query is sorted by StartTime
Dummydate = #12/25/2008#
dtNow = Dummydate + TimeValue(dtFind)
Set rs = CurrentDb.OpenRecordset(MyTable)
rs.MoveFirst
Do Until rs.EOF
MyStart = rs(MyStartField)
MyEnd = rs(MyEndField)
MyFind = rs(MyFindField)
If TimeValue(MyEnd) < TimeValue(MyStart) And dtNow < MyStart Then
'ie shift spans midnight and dtNow is after midnight
'add 1 day to dtNow
dtNow = dtNow + 1
Else
dtNow = dtNow
End If
If dtNow >= MyStart And dtNow <= MyEnd Then
ShiftFind = MyFind
GoTo FoundIt
'break out of loop
Else
rs.MoveNext
'it wasn't the right time so move to next record
End If
Loop
ShiftFind = "No shift"
End If
FoundIt:
rs.Close
Set rs = Nothing
End Function
Evi
into
TblShift has
ShiftName,-ShiftStart(time), ShiftEnd(Time)
Shift1- 09:00:00 -14:00:00
Shift2 - 14:00:01 - 16:00:00
Shift3 - 18:00:01 - 04:00:00
So if Now() = 14:30, I want the result to be "Shift2"
because 14:30 is between 14:00: and 16:00
If Now() = 08:00, I want it to read "No Shift"
Dlookup between ShiftStart and ShiftEnd worked until Midnight struck, then I
got a "No Shift" because Now() at 01:00 was no longer greater than or equal
to ShiftStart and less than or equal To ShiftEnd - it was less than both
these values.
I couldn't figure out an IIF to sift this out.
To make this work, I've added a dummy date to the shift times 25/12/2008
with the Over-midnight shiftEnd being 26/12/08
I've formatted them as General Date
This function does the trick but was there an easier way without using
Recordset (which doesn't always translate easily over different versions of
Access)?
(I've left the dtFind variant there so that the function can be used say to
look up a worker's shift by using the time he started work)
Function ShiftFind(ByVal dtFind As Variant) As String
Dim dtNow As Date
Dim Dummydate As Date
Dim MyFindField As String
Dim MyStartField As String
Dim MyEndField As String
Dim MyTable As String
Dim MyStart As Date
Dim MyEnd As Date
Dim MyFind As String
Dim rs As Recordset
Dim a As Integer
If Not IsNull(dtFind) Then
'change these names to match your Table
MyStartField = "ShiftStart"
MyEndField = "ShiftEnd"
MyFindField = "ShiftName"
MyTable = "QryShiftTest"
'Query is sorted by StartTime
Dummydate = #12/25/2008#
dtNow = Dummydate + TimeValue(dtFind)
Set rs = CurrentDb.OpenRecordset(MyTable)
rs.MoveFirst
Do Until rs.EOF
MyStart = rs(MyStartField)
MyEnd = rs(MyEndField)
MyFind = rs(MyFindField)
If TimeValue(MyEnd) < TimeValue(MyStart) And dtNow < MyStart Then
'ie shift spans midnight and dtNow is after midnight
'add 1 day to dtNow
dtNow = dtNow + 1
Else
dtNow = dtNow
End If
If dtNow >= MyStart And dtNow <= MyEnd Then
ShiftFind = MyFind
GoTo FoundIt
'break out of loop
Else
rs.MoveNext
'it wasn't the right time so move to next record
End If
Loop
ShiftFind = "No shift"
End If
FoundIt:
rs.Close
Set rs = Nothing
End Function
Evi