DoCmd.OutputTo and Loop, Delay in creating PDF is stopping the Loop (1 Viewer)

russ0670

New member
Local time
Today, 17:33
Joined
Jun 27, 2015
Messages
8
I have a access table which exports a PDF report using 'DoCmd.output' for each record in the recordset using a loop.

The query the report is based on takes around 10-15 seconds to run due to some complex calculations in the query. I think this delay is effecting the loop command as it moves on to the next record while the 'DoCmd.output' is still processing.

The below code will correctly export one report in PDF format to the specified folder but it will not continue the loop and stops executing any more code below the loop. (It does not freeze, you can continue working with the Form but the hourglass is showing as it did not run the line 'DoCmd.hourglass False'

When I remove the code to create the pdf report the loop runs perfectly.

Anyone got any ideas, basically I want the loop to wait until the report is exported fully before moving to the next record.

Code:
Public Strexclusions As String
Public StrExclusionsDetail As String
Public StrExclCount As Integer
Public Strwhere As String
Public DataLock As Boolean
Public RecordSetLoaded As String

Private Sub txtStatementInc_AfterUpdate()
   
If Me.txtStatementInc = True Then
    If MsgBox("            Press OK to prepair statements for export" & vbNewLine & "     This will take a significant time for large mailers", vbOKCancel + vbDefaultButton2, "    CONFIRM ACTION    ") = vbOK Then
        '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        '@@@@@@@@@@@...........Set Recordset........@@@@@@@@@@@
        '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        Dim MyDB As Database
        Dim MyRS As Recordset
        Dim MyRSfilter As String
        DoCmd.Hourglass True

        Set MyDB = CurrentDb
        RecordSetLoaded = "CreditControlSummary"
        MyRSfilter = "SELECT * FROM " & RecordSetLoaded & " WHERE " & Strexclusions & " And " & Strwhere
        Set MyRS = MyDB.OpenRecordset(MyRSfilter)

        '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        '@@@@@@@@@@@...........Start Loop...........@@@@@@@@@@@
        '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
            
            MyRS.MoveFirst

            Do Until MyRS.EOF
            
                Dim strstatementname As String
                Dim FSO As Object, TmpFolder As Object
                Set FSO = CreateObject("scripting.filesystemobject")
                Set TmpFolder = FSO.GetSpecialFolder(2)
                strstatementname = "Communal_Account_Statement" & MyRS![Account Number]
                DoCmd.OutputTo acOutputReport, "ccStatement", acFormatPDF, TmpFolder & _
                "\" & strstatementname & ".pdf", False
                '............................................................
                '............................................................
            
            MyRS.MoveNext

            Loop
        '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        '@@@@@@@@@@@.............End Loop...........@@@@@@@@@@@
        '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        
        MsgBox ("Export Complete")
        DoCmd.Hourglass False
    Else
        Me.txtStatementInc = False
        If Me.txtAccBalanceInc = False Then
            RecordSetLoaded = "CreditControlSummaryQuick"
        Else
        'Do Nothing
        End If
    End If
Else
End If
End Sub
 

JHB

Have been here a while
Local time
Today, 16:33
Joined
Jun 17, 2012
Messages
7,732
Try by putting in a DoEvents just before you move to the next record!
Else can't you check if the file is created and if not wait until it is created?
Do the check it in a loop and remember to put in a DoEvents here also.
 

russ0670

New member
Local time
Today, 17:33
Joined
Jun 27, 2015
Messages
8
Sorry JHB, it may just be the result of the last 12 hours coding but I have no idea how to implement your Suggestion.

Do I just put

DoEvents
MyRS.MoveNext

No idea on the below.
"Else can't you check if the file is created and if not wait until it is created?
Do the check it in a loop and remember to put in a DoEvents here also"


Dont know if this helps but the files may already be in the folder from the last export and will be overwriten by the latest report. Also there may be up to 150 reports going out so checking manually that the file was exported would not an viable option.

Again I am sorry I am sure its clear to anyone not in Coffee Overdose.
 

vbaInet

AWF VIP
Local time
Today, 15:33
Joined
Jan 22, 2010
Messages
26,374
Similar to JHB's suggestion but instead of only checking that the file has been created, check the file size until it no longer increases then break out of the loop. Use a Do Until loop and use a function for checking the file size.
 

JHB

Have been here a while
Local time
Today, 16:33
Joined
Jun 17, 2012
Messages
7,732
You're welcome, good luck! :)
 

vbaInet

AWF VIP
Local time
Today, 15:33
Joined
Jan 22, 2010
Messages
26,374
If you're outputting a much bigger pdf (as the data grows), you will need to perform a more stringent check as has already been explained. For now calling DoEvents alone will suffice but in the future (with a more larger dataset) it won't. OutputTo may be running asynchronously but it will get to a point where it hangs.
 

Users who are viewing this thread

Top Bottom