Create seperate PDF Files from a report Base on a query with more than 300 records (1 Viewer)

PJAY

New member
Local time
Today, 01:59
Joined
Dec 30, 2023
Messages
4
I am trying to create PDF files (1 File per record). but i am stuck and it is not working below is my code.

Code:
Dim FileName As String
Dim FilePath As String

'FileName = Me.Hoten
'FilePath = "D:\" & FileName & ".pdf"
'DoCmd.OutputTo acOutputReport, "IIn", acFormatPDF, FilePath

    Dim rs As Object
    Dim iAns As Integer
    Dim sFolder As String
    
    
    iAns = MsgBox("Do you want to print ALL records?" & vbCrLf & vbCrLf & _
            "   Choose 'Yes' to print all records" & vbCrLf & _
            "   Chose 'No' to print the current record", vbQuestion + vbYesNoCancel)
    If iAns = vbYes Or iAns = vbNo Then
        sFolder = BrowseForFolder("Select a Folder to save the pdf")
        If Len(Trim$(sFolder)) = 0 Then
            sFolder = Environ("UserProfile") & "\Documents\"
            MsgBox "You have not selected any folder. The pdf(s) will be saved to Documents folder.", vbInformation
        End If
    End If
    If iAns = vbYes Then
        Set rs = Me.Recordset
        With rs
            If .RecordCount < 1 Then
                Exit Sub
            End If
            .MoveFirst
            Do Until .EOF
                FileName = UCase(Me.StudentFullName & "")
                FilePath = sFolder & FileName
                
                Call forceMKDir(FilePath)
                
                FilePath = FilePath & "\" & FileName & ".pdf"
                
                'FilePath = "D:\" & FileName & ".pdf"
                'FilePath = "C:\ARNEL\" & FileName & ".pdf"
                If Len(Dir$(FilePath)) <> 0 Then
                    Kill FilePath
                End If
                'DoCmd.SetParameter "p_HOTEN", FileName
                TempVars("p_Hoten") = FileName
                DoCmd.OutputTo acOutputReport, "IIn_all", acFormatPDF, FilePath
                .MoveNext
            Loop
        End With
        On Error Resume Next
        TempVars.Remove "p_Hoten"
        On Error GoTo 0
    ElseIf iAns = vbNo Then
        FileName = UCase(Me.StudentFullName & "")
        FilePath = sFolder & FileName
        
        Call forceMKDir(FilePath)
        
        FilePath = FilePath & "\" & FileName & ".pdf"
        'FilePath = "D:\" & FileName & ".pdf"
        'FilePath = "C:\ARNEL\" & FileName & ".pdf"
        If Len(Dir$(FilePath)) <> 0 Then
            Kill FilePath
        End If
        DoCmd.OutputTo acOutputReport, "Nest-Term1-Bill", acFormatPDF, FilePath
    End If


if i select NO it create all the PDF files in one single file
but if i Choose No it give me Error :

er3.jpg
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:59
Joined
May 7, 2009
Messages
19,226
upload your db, then it would be easy to modify, than bunch of snapshot screens.
just tell us which form you have trouble.
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:59
Joined
Sep 21, 2011
Messages
14,216
Again, you have to supply the line where the error occurs, just as was asked in the UA site. :(
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 21:59
Joined
Feb 19, 2002
Messages
43,196
I added a very simple piece of code to this post that should help you.

 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 02:59
Joined
Jul 9, 2003
Messages
16,268
I describe the process in detail on my website here:-

 

Users who are viewing this thread

Top Bottom