codice:
Function LinkStudentsToNewYear() As Integer
Dim dbs As Database, rstNewYear As Recordset, rstStudents As Recordset, rstBetween As Recordset
Dim szCriteria As String
'Restituisce il riferimento al database corrente.
Set dbs = CurrentDb
'Apro il recordset degli studenti
Set rstStudents = dbs.OpenRecordset("SELECT [Tabella Alunni].* FROM [Tabella Alunni] WHERE ((([Tabella Alunni].ExAlunno)<>True))", dbOpenDynaset, dbReadOnly, dbReadOnly)
'Apro il recordset dei conti degli alunni
Set rstBetween = dbs.OpenRecordset("Tabella Conti Alunni", dbOpenDynaset, dbConsistent, dbPessimistic)
'Ciclo degli studenti
Do Until rstStudents.EOF
'Criterio di query
szCriteria = "SELECT [Tabella Piano Conti Annuale].* FROM [Tabella Piano Conti Annuale] " & _
"WHERE (((Scuola)='" & rstStudents!Scuola & "') " & _
"AND ((Sezione)='" & rstStudents!Sezione & "') " & _
"AND ((Classe)=" & rstStudents!Classe & ") " & _
"AND ((Anno)=" & [Form_Scheda Piano Conti Master].txtAnno & "));"
'Apro la tabella del piano dei conti annuale
Set rstNewYear = dbs.OpenRecordset(szCriteria, dbOpenDynaset, dbReadOnly, dbReadOnly)
'Ciclo del nuovo anno
Do Until rstNewYear.EOF
'Aggiungo un record
rstBetween.AddNew
'Valori del record
rstBetween!IDAlunno = rstStudents!IDAlunno
rstBetween!IDPianoConti = rstNewYear!IDPianoConti
rstBetween!Dovuto = rstNewYear!Dovuto
rstBetween!DovutoEuro = rstNewYear!DovutoEuro
'Memorizzo le modifiche
rstBetween.Update
'Prossimo record
rstNewYear.MoveNext
Loop
'Chiudo la tabella del piano dei conti annuale
rstNewYear.Close
'Record successivo
rstStudents.MoveNext
Loop
'Chiudo i recordset
rstStudents.Close
rstBetween.Close
'Chiudo il riferimento al database
Set dbs = Nothing
'Tutto ok
LinkStudentsToNewYear = True
End Function