Something like this, perhaps ?
Code:
Option Explicit
Private Function AddToShiftFile(daDate As Date, strData As String, Optional boClose As Boolean = False) As Integer
'
' Usage: Normal - intFile = AddToShiftFile(Now, Data to be written to the file)
' At Application Close - intFile = AddToShiftFile(Now, vbNullString, True)
'
' Outputs the data to the appropriate file depending on Time of Day
' Returns the File Number of the current file in use, or Zero when the file is closed
'
Dim daTime As Date
Dim daShifts(1, 1) As Date
Dim strSFile As String
Static intFile As Integer
Static strCurSFile As String
If Not boClose Then
daShifts(0, 0) = TimeValue("06:30:00")
daShifts(0, 1) = TimeValue("14:30:00")
daShifts(1, 0) = TimeValue("14:30:00")
daShifts(1, 1) = TimeValue("22:30:00")
daTime = TimeValue(daDate)
'
' Select the appropriate output file
' depending upon which shift we're currently in
'
Select Case True
Case daTime > daShifts(0, 0) And daTime <= daShifts(0, 1)
strSFile = "C:\MyApplication\Shift 06:30 - 14:30.txt"
Case daTime > daShifts(1, 0) And daTime <= daShifts(1, 1)
strSFile = "C:\MyApplication\Shift 14:30 - 22:30.txt"
Case Else
strSFile = "C:\MyApplication\Shift 22:30 - 06:30.txt"
End Select
'
' If this shift is the same as the current shift
' then output the data to the file
'
If strSFile = strCurSFile Then
Print #intFile, strData
Else
'
' Different shift, close the current file
' (if there was one open)
' Open up the new one, make it the current file
' and output the data
'
If intFile > 0 Then Close #intFile
intFile = FreeFile
Open strSFile For Output As intFile
strCurSFile = strSFile
Print #intFile, strData
End If
AddToShiftFile = intFile
Else
Close #intFile
AddToShiftFile = 0
End If
End Function
Not tested but hopefully you can see the logic
EDIT: You may have to remember to adjust for the Canvey Island time-shift 