Bom dia, mendes
A rotatividade está um pouco embricada. Quando um funcionário estiver de férias, todos os outros avançam? Como estabelecer o ciclo do turno? Fica sem a regra pré-estabelecida?
Depois de muito batalhar, criei-lhe este código:
'Autor Alexandre Neves
Dim DiaMes As Integer, MesTurno As Date
Dim Inicio As Date, Fim As Date
Dim Rst1 As DAO.Recordset, Rst2 As DAO.Recordset, Rst3 As DAO.Recordset
Dim AvancosPorFerias As Integer
If MsgBox("Confirmar a Rotina ? ", vbYesNo + vbQuestion, "Gestão") = vbYes Then
Inicio = Format(Me.Inicio, "mm-01-yyyy") ' Format(InputBox("Introduza a data inicial para a elaboração da escala."), "mm-01-yyyy")
Fim = Format(Me.Fim, "mm-01-yyyy") 'Format(InputBox("Introduza a data final para a elaboração da escala."), "mm-01-yyyy")
CurrentDb.Execute "DELETE * FROM HorarioInicial;"
Set Rst1 = CurrentDb.OpenRecordset("SELECT FuncionarioID, NomeTrat, Horas FROM Funcionarios;")
Set Rst2 = CurrentDb.OpenRecordset("SELECT Turno FROM Turnos;")
Set Rst3 = CurrentDb.OpenRecordset("SELECT Turno FROM Turnos1;")
Rst1.MoveLast: Rst1.MoveFirst
Rst2.MoveLast: Rst2.MoveFirst
Rst3.MoveLast: Rst3.MoveFirst
Do While Not Rst1.EOF
Rst2.MoveFirst
Rst2.Move Rst1.AbsolutePosition Mod Rst2.RecordCount
Rst3.MoveFirst
Rst3.Move Rst1.AbsolutePosition Mod Rst3.RecordCount
For MesTurno = Inicio To Fim
If Day(Format(MesTurno, "mm-dd-yyyy")) = 1 Then
For DiaMes = 1 To DateSerial(Year(Format(MesTurno, "mm-dd-yyyy")), Month(Format(MesTurno, "mm-dd-yyyy")) + 1, 1) - DateSerial(Year(Format(MesTurno, "mm-dd-yyyy")), Month(Format(MesTurno, "mm-dd-yyyy")), 1)
AvancosPorFerias = 0
If Rst2.EOF Then Rst2.MoveFirst
If Rst3.EOF Then Rst3.MoveFirst
If DiaMes = 1 Then
AvancaFerias1:
If DCount("*", "Ferias", "FuncionarioID=" & Rst1(0) & " and (#" & Format(DateSerial(Year(MesTurno), Month(Format(MesTurno, "mm-dd-yyyy")), DiaMes), "mm-dd-yyyy") & "# Between Inicio1 and Fim1 or #" & Format(DateSerial(Year(MesTurno), Month(Format(MesTurno, "mm-dd-yyyy")), DiaMes), "mm-dd-yyyy") & "# Between Inicio2 and Fim2 or #" & Format(DateSerial(Year(MesTurno), Month(Format(MesTurno, "mm-dd-yyyy")), DiaMes), "mm-dd-yyyy") & "# Between Inicio3 and Fim3 or #" & Format(DateSerial(Year(MesTurno), Month(Format(MesTurno, "mm-dd-yyyy")), DiaMes), "mm-dd-yyyy") & "# Between Inicio4 and Fim4)") > 0 Then
CurrentDb.Execute "INSERT INTO HorarioInicial(FuncionarioID,NomeTrat,Horas,Mes,1) VALUES ('" & Rst1(0) & "','" & Rst1(1) & "','" & Rst1(2) & "',#" & MesTurno & "#,'F');"
Rst1.MoveNext: AvancosPorFerias = AvancosPorFerias + 1
GoTo AvancaFerias1
End If
If Rst1!Horas = 7 Then
CurrentDb.Execute "INSERT INTO HorarioInicial(FuncionarioID,NomeTrat,Horas,Mes,1) VALUES ('" & Rst1(0) & "','" & Rst1(1) & "','" & Rst1(2) & " ',#" & MesTurno & "#,'" & Rst2(0) & "');"
Rst2.MoveNext
Else
CurrentDb.Execute "INSERT INTO HorarioInicial(FuncionarioID,NomeTrat,Horas,Mes,1) VALUES ('" & Rst1(0) & "','" & Rst1(1) & "','" & Rst1(2) & " ',#" & MesTurno & "#,'" & Rst3(0) & "');"
Rst3.MoveNext
End If
Else
AvancaFerias2:
If DCount("*", "Ferias", "FuncionarioID=" & Rst1(0) & " and (#" & Format(DateSerial(Year(MesTurno), Month(Format(MesTurno, "mm-dd-yyyy")), DiaMes), "mm-dd-yyyy") & "# Between Inicio1 and Fim1 or #" & Format(DateSerial(Year(MesTurno), Month(Format(MesTurno, "mm-dd-yyyy")), DiaMes), "mm-dd-yyyy") & "# Between Inicio2 and Fim2 or #" & Format(DateSerial(Year(MesTurno), Month(Format(MesTurno, "mm-dd-yyyy")), DiaMes), "mm-dd-yyyy") & "# Between Inicio3 and Fim3 or #" & Format(DateSerial(Year(MesTurno), Month(Format(MesTurno, "mm-dd-yyyy")), DiaMes), "mm-dd-yyyy") & "# Between Inicio4 and Fim4)") > 0 Then
CurrentDb.Execute "UPDATE HorarioInicial SET " & DiaMes & "='F' WHERE NomeTrat='" & Rst1(1) & "' and Mes=#" & MesTurno & "#;"
Rst1.MoveNext: AvancosPorFerias = AvancosPorFerias + 1
GoTo AvancaFerias2
End If
If Rst1!Horas = 7 Then
CurrentDb.Execute "UPDATE HorarioInicial SET " & DiaMes & "='" & Rst2(0) & "' WHERE NomeTrat='" & Rst1(1) & "' and Mes=#" & MesTurno & "#;"
Rst2.MoveNext
Else
CurrentDb.Execute "UPDATE HorarioInicial SET " & DiaMes & "='" & Rst3(0) & "' WHERE NomeTrat='" & Rst1(1) & "' and Mes=#" & MesTurno & "#;"
Rst3.MoveNext
End If
End If
Rst1.Move -AvancosPorFerias
Next
End If
Next
Rst1.MoveNext
Loop
Set Rst1 = Nothing: Set Rst2 = Nothing: Set Rst3 = Nothing
MsgBox " Rotina Terminada ", vbExclamation, "Gestão"
Me.Requery
Call Form_Load
Else
DoCmd.CancelEvent
Exit Sub
End If
veja se é o que pretende e como o problema não é a codificação do programa mas o seu fluxo, deve fazer, pelo menos para os casos mais complicados, um fluxograma.