Hi Everyone,
This question concerns an Access 2010 database I’m creating for one of my programs at work. I’m having an issue trying to get a nested loop to work. I am very new at loops, and at working with recordsets in vba. Here’s what I’m trying to accomplish:
The user clicks a button, and is taken to a form where they can see students who have a student record, but no student reporting record. I want the user to be able to click another button and create ten reporting records for each student on the list, one for each year from 2007-2016. Here is the code that accomplishes this:
Dim rst As DAO.Recordset
Dim i As Integer
Set rst = CurrentDb.OpenRecordset("SELECT * From qryAPR WHERE StudentID=" & Me.StudentID)
If rst.EOF Then
For i = 1 To 10
With rst
.AddNew
!StudentID = Me.StudentID
!Type = 2
!BatchAY = i
.Update
End With
Next
End If
DoCmd.RefreshRecord
This works perfectly for the first student, but only the first student. If I want to create the reporting records for the other students on the list, I have to go back and click the button each time for each one. I know there has to be a way to do this all at once, but I’m not sure how. I’ve experimented with Do While Loops, but so far I can’t get anything to work. Any help would be appreciated. Thank you!
This question concerns an Access 2010 database I’m creating for one of my programs at work. I’m having an issue trying to get a nested loop to work. I am very new at loops, and at working with recordsets in vba. Here’s what I’m trying to accomplish:
The user clicks a button, and is taken to a form where they can see students who have a student record, but no student reporting record. I want the user to be able to click another button and create ten reporting records for each student on the list, one for each year from 2007-2016. Here is the code that accomplishes this:
Dim rst As DAO.Recordset
Dim i As Integer
Set rst = CurrentDb.OpenRecordset("SELECT * From qryAPR WHERE StudentID=" & Me.StudentID)
If rst.EOF Then
For i = 1 To 10
With rst
.AddNew
!StudentID = Me.StudentID
!Type = 2
!BatchAY = i
.Update
End With
Next
End If
DoCmd.RefreshRecord
This works perfectly for the first student, but only the first student. If I want to create the reporting records for the other students on the list, I have to go back and click the button each time for each one. I know there has to be a way to do this all at once, but I’m not sure how. I’ve experimented with Do While Loops, but so far I can’t get anything to work. Any help would be appreciated. Thank you!