Solved

Access: Use of  VBA to check if pdf report is open or not

Posted on 2015-02-19
4
821 Views
Last Modified: 2016-02-10
I have a button in my form which creates and open a pdf document of my report, already filtered and sorted,  but it gives an error if you attempt to create another pdf with the previous one still open.  the error message is:

"The OutputTo action was canceled."

If the open pdf is closed, then the command runs fine.  My Question:
How can i modify my code to check if the pdf file is already open and close it, or pops a message box advising the user to close the open pdf before running the command again? here is my code for creating a pdf document from a report in my access Form.

Private Sub CmdPreviewReport_Click()

Dim stDocName As String
stDocName = "Request_Report" ' Name of Report
strLinkCriteria = Me.MainWorkloadQueryForm.form.Filter

'Opening and Previewing filtered Report
DoCmd.OpenReport stDocName, acViewPreview, , strLinkCriteria, acHidden, Me.MainWorkloadQueryForm.form.OrderBy
DoCmd.SelectObject acReport, stDocName

'Obtaining Data from filtered report and converting report to PDF using DoCmd.OutputTo
DoCmd.OutputTo acOutputReport, "Request_Report", acFormatPDF, "Request_Report.pdf", True

'Closes the Report in Access leaving just the opened PDF
DoCmd.Close acReport, stDocName, acSaveNo
End Sub
0
Comment
Question by:peterslove53
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
Nick67 earned 500 total points
ID: 40619902
This works. Pass in a full path to a file
From http://support.microsoft.com/kb/291295
Tested in Access 2003 with a pdf file

' This function checks to see if a file is open or not. If the file is
' already open, it returns True. If the file is not open, it returns
' False. Otherwise, a run-time error occurs because there is
' some other problem accessing the file.

Function IsFileOpen(filename As String)
    Dim filenum As Integer, errnum As Integer

    On Error Resume Next   ' Turn error checking off.
    filenum = FreeFile()   ' Get a free file number.
    ' Attempt to open the file and lock it.
    Open filename For Input Lock Read As #filenum
    Close filenum          ' Close the file.
    errnum = Err           ' Save the error number that occurred.
    On Error GoTo 0        ' Turn error checking back on.

    ' Check to see which error occurred.
    Select Case errnum

        ' No error occurred.
        ' File is NOT already open by another user.
        Case 0
         IsFileOpen = False

        ' Error number for "Permission Denied."
        ' File is already opened by another user.
        Case 70
            IsFileOpen = True

        ' Another error occurred.
        Case Else
            Error errnum
    End Select

End Function

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 40619949
Change the name of the PDF to include a timestamp
Example: Request_Report_20150219164235.PDF
0
 

Author Comment

by:peterslove53
ID: 40619995
Hi aikimark, That is also a good idea!
0
 

Author Comment

by:peterslove53
ID: 40619999
I finally decided to add a message box telling the user another report was open and needs to be closed. This seems to suit me:

Private Sub CmdPreviewReport_Click()
On Error GoTo Err_CmdPreviewReport_Click

Dim stDocName As String
stDocName = "Request_Report" ' Name of Report
strLinkCriteria = Me.MainWorkloadQueryForm.form.Filter

'Opening and Previewing filtered Report
DoCmd.OpenReport stDocName, acViewPreview, , strLinkCriteria, acHidden, Me.MainWorkloadQueryForm.form.OrderBy
DoCmd.SelectObject acReport, stDocName



'Obtaining Data from filtered report and converting report to PDF using DoCmd.OutputTo
DoCmd.OutputTo acOutputReport, "Request_Report", acFormatPDF, "Request_Report.pdf", True

'Closes the Report in Access leaving just the opened PDF
DoCmd.Close acReport, stDocName, acSaveNo

Exit_CmdPreviewReport_Click:
    Exit Sub

Err_CmdPreviewReport_Click:
    MsgBox "The Report Is open already!" & vbCrLf & "Close the report and try again", , "Close Open Report"
    'MsgBox Err.Description
    DoCmd.Close acReport, stDocName, acSaveNo
    Resume Exit_CmdPreviewReport_Click
End Sub
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now