Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-02-19
4
Medium Priority
?
1,272 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 1500 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 46

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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a serious pitfall that can happen when deleting shapes using VBA.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

877 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