Solved

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

Posted on 2015-02-19
4
1,097 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Use Windows Task Scheduler to print a Word document weekly so your printer ink won't dry out.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

623 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