I'm creating a scheduling application for a hospital department that indicates when they've exceeded the minimum required staff for a particular shift. I have a table keeping track of the count and another table that contains the miniumum values based on specific shifts and roles. For example, I know the exact number of staff needed at 2 AM, and I have a staff member working from 11PM through 7AM so I need to indicate that this person will fulfill one of the required slots at 2AM. I also need to account for staff carrying over into a different day, as indicated in this example, so I have to create counts for multiple days (as in day before scheduled date or day after scheduled date). I need to provide this information for multiple days. When I created the information for the initial day, I started getting the non-descript "Microsoft Access has encountered a problem and needs to close. We are sorry for the inconvenience." I embedded a requery command in the code and it all functions correctly for the first day. When I try and enter information on subsequent days, I get an error that the requery command is creating a duplicate entry in the database. Since this code finds current entries for a role and a shift then adds one to the count, I don't understand how it could be creating duplicate values. Here's the vb code for the module:
Any assistance with this would be greatly appreciated!
Thanks
Code:
Option Compare Database
'OpenArgs:=Me.ScheduleDate & "|" & Me.StaffRole & "|" & Me.StaffShift
Private Sub Form_Load()
DoCmd.Requery
Dim TempRole As Integer
Dim strSQL As String
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim varSplit As Variant
Dim FinalLoop, LoopCount, TempAMG, TempARN, TempCAR, TempCCA, TempCRN, TempEDU, TempEMT, TempIT, TempLPN, TempMGR, TempPRR, TempRN, TempUC As Integer
Dim Starting As String
Dim OriginalDate As Date
Dim tsd As Date
Dim OriginalDatePlusOne As Date
If Me.OpenArgs <> vbNullString Then
varSplit = Split(Me.OpenArgs, "|")
OriginalDate = varSplit(0)
OriginalDatePlusOne = DateAdd("D", 1, varSplit(0))
TempRole = varSplit(1)
End If
'if schedule date exists
If DCount("[ScheduleDate]", "CoreScheduleCountOfPosition", "[ScheduleDate] = #" & OriginalDate & "#") Then
Me.ScheduleDate = varSplit(0)
tsd = DateAdd("d", 1, OriginalDate)
'if schedule date + 1 exists
If DCount("[ScheduleDate]", "CoreScheduleCountOfPosition", "[ScheduleDate] = #" & tsd & "#") Then
FinalLoop = DLookup("Duration", "ListOfShifts", "ID =" & varSplit(2)) + 1
Starting = DLookup("StartTime", "ListOfShifts", "ID =" & varSplit(2))
DoCmd.Requery
Me.Recordset.FindFirst "[ScheduleDate] = #" & OriginalDate & "# and [TimeSlot] = '" & Starting & "'"
' End If
'update loop
For LoopCount = 1 To FinalLoop
Call Initialize
If TempRole = 1 Then
Me.ManagerCount = TempMGR + 1
End If
If TempRole = 2 Then
Me.AsstManagerCount = TempAMG + 1
End If
If TempRole = 3 Then
Me.EducCount = TempEDU + 1
End If
If TempRole = 4 Then
Me.ITCount = TempIT + 1
End If
If TempRole = 5 Then
Me.PRRCount = TempPRR + 1
End If
If TempRole = 6 Then
Me.CARCount = TempCAR + 1
End If
If TempRole = 7 Then
Me.ARNCount = TempARN + 1
End If
If TempRole = 8 Then
Me.CRNCount = TempARN + 1
End If
If TempRole = 9 Then
Me.RNCount = TempRN + 1
End If
If TempRole = 10 Then
Me.LPNCount = TempLPN + 1
End If
If TempRole = 11 Then
Me.EMTCount = TempEMT + 1
End If
If TempRole = 12 Then
Me.UCCount = TempUC + 1
End If
If TempRole = 13 Then
Me.CCACount = TempCCA + 1
End If
Me.CSCStatus = "W"
If LoopCount <= FinalLoop Then
DoCmd.GoToRecord , , acNext
End If
Next LoopCount
Else:
'create and update second set if it doesn't exist
Dim tc As Integer
For k = 1 To 24
If k >= 1 And k <= 10 Then
Me.TimeSlot = "0" & k - 1 & ":00"
Else:
Me.TimeSlot = k - 1 & ":00"
End If
Me.ScheduleDate = tsd
Call SetToZero
Me.CSCStatus = "W"
If k <= 24 Then
DoCmd.GoToRecord , , acNewRec
End If
Next k
FinalLoop = DLookup("Duration", "ListOfShifts", "ID =" & varSplit(2)) + 1
Starting = DLookup("StartTime", "ListOfShifts", "ID =" & varSplit(2))
Me.Recordset.FindFirst "[ScheduleDate] = #" & varSplit(0) & "# and [TimeSlot] = '" & Starting & "'"
For LoopCount = 1 To FinalLoop
Call Initialize
If TempRole = 1 Then
Me.ManagerCount = TempMGR + 1
End If
If TempRole = 2 Then
Me.AsstManagerCount = TempAMG + 1
End If
If TempRole = 3 Then
Me.EducCount = TempEDU + 1
End If
If TempRole = 4 Then
Me.ITCount = TempIT + 1
End If
If TempRole = 5 Then
Me.PRRCount = TempPRR + 1
End If
If TempRole = 6 Then
Me.CARCount = TempCAR + 1
End If
If TempRole = 7 Then
Me.ARNCount = TempARN + 1
End If
If TempRole = 8 Then
Me.CRNCount = TempARN + 1
End If
If TempRole = 9 Then
Me.RNCount = TempRN + 1
End If
If TempRole = 10 Then
Me.LPNCount = TempLPN + 1
End If
If TempRole = 11 Then
Me.EMTCount = TempEMT + 1
End If
If TempRole = 12 Then
Me.UCCount = TempUC + 1
End If
If TempRole = 13 Then
Me.CCACount = TempCCA + 1
End If
Me.CSCStatus = "W"
If LoopCount <= FinalLoop Then
DoCmd.GoToRecord , , acNext
End If
Next LoopCount
End If
Else:
'create if second exists but first doesn't
DoCmd.GoToRecord , , acNewRec
If DCount("[ScheduleDate]", "CoreScheduleCountOfPosition", "[ScheduleDate] = #" & OriginalDatePlusOne & "#") Then
For i = 1 To 24
Me.ScheduleDate = OriginalDate
If i >= 1 And i <= 10 Then
Me.TimeSlot = "0" & (i - 1) & ":00"
Else:
Me.TimeSlot = (i - 1) & ":00"
End If
Call SetToZero
Me.CSCStatus = "W"
If i <= 24 Then
DoCmd.GoToRecord , , acNewRec
End If
Next i
FinalLoop = DLookup("Duration", "ListOfShifts", "ID =" & varSplit(2)) + 1
Starting = DLookup("StartTime", "ListOfShifts", "ID =" & varSplit(2))
Me.Recordset.FindFirst "[ScheduleDate] = #" & varSplit(0) & "# and [TimeSlot] = '" & Starting & "'"
For LoopCount = 1 To FinalLoop
Call Initialize
If TempRole = 1 Then
Me.ManagerCount = TempMGR + 1
End If
If TempRole = 2 Then
Me.AsstManagerCount = TempAMG + 1
End If
If TempRole = 3 Then
Me.EducCount = TempEDU + 1
End If
If TempRole = 4 Then
Me.ITCount = TempIT + 1
End If
If TempRole = 5 Then
Me.PRRCount = TempPRR + 1
End If
If TempRole = 6 Then
Me.CARCount = TempCAR + 1
End If
If TempRole = 7 Then
Me.ARNCount = TempARN + 1
End If
If TempRole = 8 Then
Me.CRNCount = TempARN + 1
End If
If TempRole = 9 Then
Me.RNCount = TempRN + 1
End If
If TempRole = 10 Then
Me.LPNCount = TempLPN + 1
End If
If TempRole = 11 Then
Me.EMTCount = TempEMT + 1
End If
If TempRole = 12 Then
Me.UCCount = TempUC + 1
End If
If TempRole = 13 Then
Me.CCACount = TempCCA + 1
End If
Me.CSCStatus = "W"
If LoopCount <= FinalLoop Then
If Me.TimeSlot = "23:00" Then
Me.Recordset.FindFirst "[ScheduleDate] = #" & OriginalDatePlusOne & "# and [TimeSlot] = '00:00'"
Else:
DoCmd.GoToRecord , , acNext
End If
End If
Next LoopCount
Else:
'create both sets if neither exists
For i = 1 To 24
Me.ScheduleDate = OriginalDate
If i >= 1 And i <= 10 Then
Me.TimeSlot = "0" & (i - 1) & ":00"
Else:
Me.TimeSlot = (i - 1) & ":00"
End If
Call SetToZero
If i <= 24 Then
DoCmd.GoToRecord , , acNewRec
End If
Next i
For i = 1 To 24
Me.ScheduleDate = OriginalDatePlusOne
If i >= 1 And i <= 10 Then
Me.TimeSlot = "0" & (i - 1) & ":00"
Else:
Me.TimeSlot = (i - 1) & ":00"
End If
Call SetToZero
Me.CSCStatus = "W"
If i <= 24 Then
DoCmd.GoToRecord , , acNewRec
End If
Next i
FinalLoop = DLookup("Duration", "ListOfShifts", "ID =" & varSplit(2)) + 1
Starting = DLookup("StartTime", "ListOfShifts", "ID =" & varSplit(2))
Me.Recordset.FindFirst "[ScheduleDate] = #" & varSplit(0) & "# and [TimeSlot] = '" & Starting & "'"
For LoopCount = 1 To FinalLoop
Call Initialize
If TempRole = 1 Then
Me.ManagerCount = TempMGR + 1
End If
If TempRole = 2 Then
Me.AsstManagerCount = TempAMG + 1
End If
If TempRole = 3 Then
Me.EducCount = TempEDU + 1
End If
If TempRole = 4 Then
Me.ITCount = TempIT + 1
End If
If TempRole = 5 Then
Me.PRRCount = TempPRR + 1
End If
If TempRole = 6 Then
Me.CARCount = TempCAR + 1
End If
If TempRole = 7 Then
Me.ARNCount = TempARN + 1
End If
If TempRole = 8 Then
Me.CRNCount = TempARN + 1
End If
If TempRole = 9 Then
Me.RNCount = TempRN + 1
End If
If TempRole = 10 Then
Me.LPNCount = TempLPN + 1
End If
If TempRole = 11 Then
Me.EMTCount = TempEMT + 1
End If
If TempRole = 12 Then
Me.UCCount = TempUC + 1
End If
If TempRole = 13 Then
Me.CCACount = TempCCA + 1
End If
Me.CSCStatus = "W"
If LoopCount <= FinalLoop Then
DoCmd.GoToRecord , , acNext
End If
Next LoopCount
End If
End If
'End If
DoCmd.Requery
DoCmd.Close acForm, "BuildScheduleCount2"
End Sub
Public Sub Initialize()
TempAMG = Me.AsstManagerCount
TempARN = Me.ARNCount
TempCAR = Me.CARCount
TempCCA = Me.CCACount
TempCRN = Me.CRNCount
TempEDU = Me.EducCount
TempEMT = Me.EMTCount
TempIT = Me.ITCount
TempLPN = Me.LPNCount
TempMGR = Me.ManagerCount
TempPRR = Me.PRRCount
TempRN = Me.RNCount
TempUC = Me.UCCount
End Sub
Public Sub SetToZero()
Me.ManagerCount = 0
Me.AsstManagerCount = 0
Me.EducCount = 0
Me.ITCount = 0
Me.PRRCount = 0
Me.CARCount = 0
Me.ARNCount = 0
Me.CRNCount = 0
Me.RNCount = 0
Me.LPNCount = 0
Me.EMTCount = 0
Me.UCCount = 0
Me.CCACount = 0
End Sub
Thanks
Comment