Loop stops halfway #2 while trying to save pdf

Hakello

Registered User.
Local time
Today, 22:50
Joined
Apr 18, 2013
Messages
23
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

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?
 
Problem is you are continually appending to stOutput. Try:

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
    [COLOR="Red"]stFolderNaam [/COLOR]= 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 = [COLOR="red"]stFolderNaam [/COLOR]& "\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

As spikepl mentioned, it's well worth investing a bit of time learning the debugging tools.

Chris
 
:banghead: duh. Sorry, I guess that was pretty obvious :( Thats what you get when someone asks to change something you made ages ago... Well, at least you made some teachers pretty happy :)

You are right about the debugging, I'll take a better look myself next time, its just that I am a little tired and gave up way to quickly. Thanks for the help guys!
 

Users who are viewing this thread

Back
Top Bottom