Ciao Vladimiro,
prova a valutare la seguente funzione. Ci sono alcune cose che non tornano rispetto alle immagini che hai pubblicato ma in alcuni casi, ammesso che abbia capito bene il meccanismo, ho l'impressione che l'errore non sia nella funzione.
Diciamo che vuole essere un inizio :-)
Public Function fCalcolaRitardi(ByVal dData As Date, ByVal iOra As Integer) As Integer
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim iRitardo As Integer
Dim iContaP As Integer
Dim dDataPrec As Date
Dim sGGprec As String
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT Data, Ora, GG From tRitardi " _
& "WHERE Data <= # " & Format(dData, "mm/dd/yyyy") & "# " _
& "AND Ora <= " & iOra & " Order By Data, Ora", dbOpenSnapshot, dbReadOnly)
With rst
Do While Not .EOF
'Debug.Print !Data; !Ora
If !GG = "P" Then
If dDataPrec = !Data And sGGprec = "P" Then
iContaP = iContaP + 1
Else
iRitardo = 0
End If
ElseIf !GG = "V" Then
If dDataPrec = !Data And sGGprec = "P" Then
iRitardo = iContaP + 1
ElseIf dDataPrec = !Data And sGGprec = "V" Then
iRitardo = 0
Else
iRitardo = 1
End If
End If
dDataPrec = !Data
sGGprec = !GG
.MoveNext
Loop
End With
fCalcolaRitardi = iRitardo
rst.Close
db.Close
Set rst = Nothing
Set db = Nothing
End Function
David