I made the following vba command to save a pdf for every student in a particular group. However, when executing the code the loop stops halfway the second iteration and I'm not sure why. I added some debugging msgboxes and it dies the second time it gets to
Full code:
Does anybody know why this is happening or what I could try to prevent this?
Code:
DoCmd.OutputTo acReport, stRap, "PDF Format(*.pdf)", stOutput, False, ""
Full code:
Code:
Private Sub btnPDF_Click()
Dim stRap As String
Dim stGroep As String
Dim stFilter As String
Dim stOutput As String
Dim stFolderNaam As String
Dim rs As DAO.Recordset
Dim stNaam As String
stRap = "RAP_Cijferlijst"
stGroep = Me.tbGroepcode
Set rs = CurrentDb.OpenRecordset("SELECT studentindex, studentvn, studenttv, studentan FROM Student WHERE groepcode = '" & stGroep & "';")
'Selecteer folder via module 'Select Folder' en sla op
stOutput = BrowseFolder("Selecteer een map om document in op te slaan")
'Check to see if the recordset actually contains rows
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
Do Until rs.EOF = True
'Set variables
stFilter = "[studentindex] = " & rs!studentindex & "AND [rapport] = True"
stNaam = rs!studentvn & " " & rs!studenttv & " " & rs!studentan
stOutput = stOutput & "\Cijferlijst_" & stNaam & ".pdf"
'Sla pdf op
DoCmd.OpenReport stRap, acViewPreview, , stFilter, acHidden
MsgBox "I reach this on #2"
DoCmd.OutputTo acReport, stRap, "PDF Format(*.pdf)", stOutput, False, ""
MsgBox "I don't reach this on #2"
DoCmd.Close acReport, stRap
'Move to the next record.
rs.MoveNext
Loop
Else
MsgBox "Er lijkt niks te zijn om op te slaan"
End If
rs.Close 'Close the recordset
Set rs = Nothing 'Clean up
End Sub
Does anybody know why this is happening or what I could try to prevent this?