Results 1 to 4 of 4
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919

    Error 2046 - OutputTo strikes AGAIN

    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


  2. #2
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I have a report (actually several) that I save as a PDF. Using a list box, I select the employees that I want to have a PDF for (1 to ??), then click the Export button. Never had a problem
    Here is a snippet of my code:
    Code:
        Set frm = Forms!reportlist
        Set ctl = frm!lstEmp
    
        For Each varItm In ctl.ItemsSelected
            tmpLast = vbNullString
            tmpFirst = vbNullString
            tmpMI = vbNullString
            Me.ubEndPK = vbNullString
    
            tmpLast = ctl.Column(2, varItm)
            tmpFirst = ctl.Column(3, varItm)
            tmpMI = ctl.Column(4, varItm)
            Me.ubEndPK = ctl.ItemData(varItm)
    
            strSaveFileName = sPath & "\" & tmpLast & "_" & tmpFirst & "_" & tmpMI & ".pdf"   '<<-- generate the PDF filename on-the-fly
            DoCmd.OutputTo acOutputReport, Me.lstForm, acFormatPDF, strSaveFileName, False    '<<-- Export to PDF
    
        Next varItm
    Me.lstForm is the listbox that has the report name.

    When the report is output, the records are filtered by date range using controls on the Forms!reportlist.

    I have never opened the report before exporting to PDF.

    BTW, the "acSaveNo" parameter for the DoCmd is only if you made changes to the DESIGN of the report using VBA. Otherwise it is ignored.



    In your code, I would remove the DoCmd.OpenReport and the DoCmd.Close (acReport) commands.

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Thanks Steve. As I mentioned in my resolving post, I had commented out the OpenReport to resolve the OP. I've now also removed the un-needed Close as well.
    Thanks again,
    Bill

  4. #4
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Glad you go tit sorted out.....

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Class module getting a 2046 error
    By GraeagleBill in forum Programming
    Replies: 13
    Last Post: 11-09-2021, 02:23 PM
  2. Replies: 12
    Last Post: 05-18-2017, 10:15 AM
  3. Replies: 3
    Last Post: 02-08-2017, 03:26 PM
  4. Run-time error 2046
    By Evans2 in forum Forms
    Replies: 8
    Last Post: 09-06-2014, 01:52 PM
  5. Replies: 13
    Last Post: 10-19-2012, 06:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums