I have a form where one of the combo options is to send a report to a pdf file directly. The first time I exercise the option, I get error 2046, OutputTo is not currently available. If I do nothing else other than to dropdown the combo and choose the same option again everything works fine. My code in a general module is below: (rptName and PDFName are passed from the form containing the combo where the option is appears.)
Am I missing something perhaps related to timing? A DoEvent after the OpenReport had no affect on issue.
I did review the thread https://www.accessforums.net/showthread.php?t=84728 where a similar problem was solved running the code in a class module, but that resolve doesn't seem to fit a mouse down on a combo option like it did on a label unless I'm just not familiar enough with what can be done with class modules?
UPDATE: I again studied the referenced thread above and in that there is no dynamic filtering done in the report, I commented out the OpenReport statement and the OP issue went away completely.
Code:
Option Compare Database
Option Explicit
Public Sub pdfDirect(rptName As String, PDFName As String)
On Error GoTo Err_Handler
DoCmd.OpenReport rptName, acViewPreview, , , acHidden
DoCmd.OutputTo acOutputReport, rptName, acFormatPDF, PDFName
DoCmd.Close acReport, rptName, acSaveNo
Call rptShow(PDFName)
Exit_Handler:
Exit Sub
Err_Handler:
MsgBox "Error " & Err.Number & " in attempting to create pdf file. " & Err.Description
Resume Exit_Handler
End Sub
Public Sub rptShow(FileName As String)
Dim ShellRC As Variant
Dim ShellString As String
If Len(Dir(FileName)) > 0 Then
ShellString = "c:\Windows\Explorer.exe " & """" & FileName & """"
ShellRC = Shell(ShellString, 4)
Else
MsgBox "PDF file " & FileName & " doesn't exist."
End If
End Sub