Creating pdf’s with Intermittent problem (1 Viewer)

June7

AWF VIP
Local time
Yesterday, 23:16
Joined
Mar 9, 2014
Messages
5,501
Yes, I posted code prematurely. Already edited post and all good now.
 

earls

Registered User.
Local time
Today, 03:16
Joined
Mar 27, 2018
Messages
21
Following revised code works for me:
Code:
Private Sub cmdSaveAsPDF_Click()
Dim qdf As DAO.QueryDef
Dim strPathName As String
Dim rs As Recordset
Dim stDocName As String
Dim strSavedSQL As String
If Me.Dirty Then Me.Dirty = False
stDocName = "xinvoice"
Set rs = CurrentDb.OpenRecordset("SELECT orderid, tripname FROM Orders WHERE SelectedPrint;", dbOpenSnapshot)
If rs.EOF Then
    MsgBox "Nothing found to process", vbCritical, "Error"
Else
    CreateFolder CurrentProject.Path & "\orders"
    ' store the current SQL
    Set qdf = CurrentDb.QueryDefs("Invoices")
    strSavedSQL = qdf.SQL
    While Not rs.EOF
        qdf.SQL = Left(strSavedSQL, InStr(strSavedSQL, ";") - 1) & " and (orderid = " & rs!OrderID & ");"
        ' put in the same folder as the database
        strPathName = CurrentProject.Path & "\orders\" & rs!tripname & ".pdf"
        DoCmd.OutputTo acOutputReport, stDocName, acFormatPDF, strPathName
        rs.MoveNext
    Wend
    ' restore the SQL
    qdf.SQL = strSavedSQL
    qdf.Close
    Set qdf = Nothing
End If
rs.Close
Set rs = Nothing
End Sub

A million thank's. worked perfect, just did a large scare run with no issues and perfect results.:D
 

isladogs

MVP / VIP
Local time
Today, 08:16
Joined
Jan 14, 2017
Messages
18,268
Yes, I posted code prematurely. Already edited post and all good now.

That'll teach me to be quick on the draw!
BUT Unfortunately it still errors for me when records selected
Definitely logging off now

Glad it works for the OP which is much more important!
 

Cronk

Registered User.
Local time
Today, 17:16
Joined
Jul 4, 2013
Messages
2,774
The problem with June's code is that the report is based on a saved query. Opening an instance of the querydef (qdf) and changing its sql, does not impact on the original saved query. The saved query has to be updated which can be achieved by
Code:
qdf.close: set qdf = nothing
The OP stores the initial sql in strSavedSQL so as to be able to restore the query when the pdf's have been generated
 

earls

Registered User.
Local time
Today, 03:16
Joined
Mar 27, 2018
Messages
21
Following revised code works for me:


The code is running perfect 95% of the time, every once and a while it won’t save than going forward it will not work until I manually clear out the stored criteria in the query (invoices). What I want to do is clear the stored orderID in the query before running the “save as pdf” code (in case of past error). Any suggestions would be appreciated.
Code:
Public Sub cmdSaveAsPDF_Click()

Dim qdf As DAO.QueryDef
Dim strPathName As String
Dim rs As Recordset
Dim stDocName As String
Dim strSavedSQL As String
If Me.Dirty Then Me.Dirty = False
stDocName = "xinvoice"
Set rs = CurrentDb.OpenRecordset("SELECT orderid, tripname FROM Orders WHERE SelectedPrint and filecreated=no;", dbOpenSnapshot)
If rs.EOF Then
    MsgBox "Nothing found to process", vbCritical, "Error"
Else
    'CreateFolder CurrentProject.Path & "\orders"
    ' store the current SQL
    Set qdf = CurrentDb.QueryDefs("Invoices")
    
    strSavedSQL = qdf.SQL
    While Not rs.EOF
        qdf.SQL = Left(strSavedSQL, InStr(strSavedSQL, ";") - 1) & " and (orderid = " & rs!OrderID & ");"
        ' put in the same folder as the database
        strPathName = "c:\LCTrips\" & rs!Tripname & ".pdf"
        DoCmd.OutputTo acOutputReport, stDocName, acFormatPDF, strPathName
 
        rs.MoveNext
    Wend
    ' restore the SQL
    qdf.SQL = strSavedSQL
    qdf.Close
    Set qdf = Nothing
End If
rs.Close
Set rs = Nothing
End Sub
 

Attachments

  • 1.PNG
    1.PNG
    6 KB · Views: 49

Cronk

Registered User.
Local time
Today, 17:16
Joined
Jul 4, 2013
Messages
2,774
Why not just take the simple approach and generate the query's sql from scratch.


Code:
set qdf = db.querydefs("Invoices")
qdf.sql ="select <fields> from Orders where orderID=" & rs!OrderID
set qdf = nothing
<run the report, etc>


If you are use your Invoices query elsewhere, use a query that is unique to this reporting routine.
 

June7

AWF VIP
Local time
Yesterday, 23:16
Joined
Mar 9, 2014
Messages
5,501
Under what condition does the code get interrupted by error? Really need to figure out what causes that and deal with. And/or incorporate error handler code in the procedure that will allow clean exit of the procedure and reset the query.

Code:
...
On Error GoTo ErrProc
If Me.Dirty Then Me.Dirty = False
stDocName = "xinvoice"
Set rs = CurrentDb.OpenRecordset("SELECT orderid, tripname FROM Orders WHERE SelectedPrint;", dbOpenSnapshot)
If rs.EOF Then
...
End If
rs.Close
Set rs = Nothing
 
ExitProc:
    ' restore the SQL
    If Not qdf Is Nothing Then
        qdf.SQL = strSavedSQL
        qdf.Close
        Set qdf = Nothing
    End If
    Exit Sub
ErrProc:
    Resume ExitProc
Review http://allenbrowne.com/ser-23a.html

Cronk's suggestion is an option, however, repeatedly creating/deleting objects causes database bloat. Regardless, should run Compact & Repair periodically.
 
Last edited:

earls

Registered User.
Local time
Today, 03:16
Joined
Mar 27, 2018
Messages
21
Under what condition does the code get interrupted by error? Really need to figure out what causes that and deal with. And/or incorporate error handler code in the procedure that will allow clean exit of the procedure and reset the query.

Code:
...
On Error GoTo ErrProc
If Me.Dirty Then Me.Dirty = False
stDocName = "xinvoice"
Set rs = CurrentDb.OpenRecordset("SELECT orderid, tripname FROM Orders WHERE SelectedPrint;", dbOpenSnapshot)
If rs.EOF Then
...
End If
rs.Close
Set rs = Nothing
 
ExitProc:
    ' restore the SQL
    If Not qdf Is Nothing Then
        qdf.SQL = strSavedSQL
        qdf.Close
        Set qdf = Nothing
    End If
    Exit Sub
ErrProc:
    Resume ExitProc
Review http://allenbrowne.com/ser-23a.html

Cronk's suggestion is an option, however, repeatedly creating/deleting objects causes database bloat. Regardless, should run Compact & Repair periodically.

Initially was getting periotic errors due to font in report (was using Calibri Font), three-page pdf’s became 500kb each. Changed font to Times New Roman now reports are 8kb and running perfect on local computer in batches of 50 reports (my computer), but when running on VM it has periotic issues (the database is split but tables local on VM, no shared tables with others). Im thinking it’s the VM (power issue) or user is doing something odd. I will be visiting the user in 6 days, I’m hoping for an easy fix until I can get on site and troubleshoot.
 

Cronk

Registered User.
Local time
Today, 17:16
Joined
Jul 4, 2013
Messages
2,774
Cronk's suggestion is an option, however, repeatedly creating/deleting objects causes database bloat


Um, my post did not provide for deleting anything, only changing the sql for an existing query. I was suggesting not restoring to the original sql.
 

earls

Registered User.
Local time
Today, 03:16
Joined
Mar 27, 2018
Messages
21
Why not just take the simple approach and generate the query's sql from scratch.


Code:
set qdf = db.querydefs("Invoices")
qdf.sql ="select <fields> from Orders where orderID=" & rs!OrderID
set qdf = nothing
<run the report, etc>


If you are use your Invoices query elsewhere, use a query that is unique to this reporting routine.

I took your suggestion, seems to work will know more tomorrow after end-user test.
Thanks.
 

Users who are viewing this thread

Top Bottom