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

    Class module getting a 2046 error

    I have a class module where I'm getting a: Click image for larger version. 
<br /><script async src=
    Name: 000.jpg  Views: 27  Size: 7.8 KB  ID: 46565" class="thumbnail" style="float:CONFIG" />

    Code:
    Option Compare Database
    Option Explicit
    Dim ShellString As String
    Dim ShellRC As Variant
    Public WithEvents Target As Access.CommandButton
    
    
    Private Sub Target_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    
    
    If Me.Target.Name = "cmdPrintRegistry" Then
        strCurRptsPath = "c:\Asilomar\PDF-Reports"
        strCurPDFName = strCurRptsPath & "\ForestLodgeRegistry.pdf"
        
        DoCmd.OpenReport "rptEntireRegister", acViewPreview, , , acHidden
        DoCmd.OutputTo acOutputReport, "rptEntireRegister", acFormatPDF, strCurPDFName
        DoCmd.Close acReport, "rptEntireRegister", acSaveNo
        
        ShellString = "c:\Windows\Explorer.exe " & """" & strCurPDFName & """"
        ShellRC = Shell(ShellString, 4)
    End If
    
    
    End Sub
    I simply want to write a report to a pdf file without previewing the report on the screen. The mode of the form with the command button is in Dialog mode, but I have other class modules in much the same condition, so I'm at a loss as to why the OutputTo would be unavailable.

    Any ideas?

  2. #2
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Haven't done this in a while, so have to ask - why open the report? IIRC you don't have to, and if you're not going to review it first there's no need to open it at all?
    If sticking with the current approach, I guess I'd try DoEvents between the output and the close.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    so have to ask - why open the report?
    Same issue as described here. See posts 5,6 & 7
    https://www.accessforums.net/showthread.php?t=84719

    I don't follow your suggestion? The 2046 occurs on the OutputTO trying to write the open report to a pdf file, so DoEvents would be after the error had already occurred.

  4. #4
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    The 2046 occurs on the OutputTO trying to write the open report to a pdf file
    I was thinking possibly because it just closed in the very next line? Perhaps not since this works elsewhere. Of course, you would have stepped through this code, not to see which line errors but to see if timing was an issue, because by now you would be well aware of that troubleshooting technique. My guess was based on you only mentioning the error, which does not prove that there is anything wrong with output line itself.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    I take it you can create files in that folder?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    I substituted the troublesome report name in the app's form that deals with all the other reports that use a common class module and the same code in the OP works fine. There has to be something peculiar with the host form in this case.

    Here's the code from the form's OnLoad that creates the CommandButton collection in the host form. I'll try changing the collection to the form's labels, let the command button masquerade as such and use the common class module to handle the pdf creation. That then, should match the substitution I tried earlier.
    Code:
    Dim L As clsCommandButton
    Dim C As Control
    
    
    'Keep an instance of the clsCommandButton for each command button in the form.
    Set mcolCmdButns = New Collection
    For Each C In Me.Controls
        If TypeName(C) = "CommandButton" Then
            C.OnClick = "[Event Procedure]"
            C.OnMouseDown = "[Event Procedure]"
            C.ControlTipText = "Click on " & C.Name
            Set L = New clsCommandButton
            Set L.Target = C
            mcolCmdButns.Add L, C.Name
        End If
    Next C

  7. #7
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    Yes, see post #6

  8. #8
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    As I somewhat expected, the idea stated in #6 really doesn't change anything switching from a command button to a label if the host form's mode is such that OutputTo isn't available. I'll keep looking.

  9. #9
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    PMFJI - But you only need to open it (the report) first to apply a dynamic filter.
    Which if read you code correctly you are not doing in this instance.

    So I could be a bit confused?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by Minty View Post
    PMFJI - But you only need to open it (the report) first to apply a dynamic filter.
    Which if read you code correctly you are not doing in this instance.

    So I could be a bit confused?
    Me too, as according to the linked post 'works like a champ'
    All that was
    Code:
    Private Sub pdfDirect(rptName As String, CtrlName As String)
    
    
    strpdfFile = Forms("frmDocuments").Controls(Replace(CtrlName, "lbl", "tb"))
    strCurPDFName = strCurRptsPath & "\" & strCurRetreat & "\" & strpdfFile & ".pdf"
    
    
    DoCmd.OpenReport rptName, acViewPreview, , , acHidden
    DoCmd.OutputTo acOutputReport, rptName, acFormatPDF, strCurPDFName
    DoCmd.Close acReport, rptName, acSaveNo
    
    
    End Sub
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    Now I'm confused, see OP code showing the open report to effect the dynamic filtering
    Code:
    Option Compare Database
    Option Explicit
    Dim ShellString As String
    Dim ShellRC As Variant
    Public WithEvents Target As Access.CommandButton
    
    
    Private Sub Target_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    
    
    If Me.Target.Name = "cmdPrintRegistry" Then
        strCurRptsPath = "c:\Asilomar\PDF-Reports"
        strCurPDFName = strCurRptsPath & "\ForestLodgeRegistry.pdf"
        
        DoCmd.OpenReport "rptEntireRegister", acViewPreview, , , acHidden  <<<<<<<<<< Open report to populate the dynamics
        DoCmd.OutputTo acOutputReport, "rptEntireRegister", acFormatPDF, strCurPDFName
        DoCmd.Close acReport, "rptEntireRegister", acSaveNo
        
        ShellString = "c:\Windows\Explorer.exe " & """" & strCurPDFName & """"
        ShellRC = Shell(ShellString, 4)
    End If
    
    
    End Sub
    I substituted the troublesome report name in the app's form that deals with all the other reports that use a common class module and the same code in the OP works fine
    Works great when functionality is launched from same form as the one for all the other reports. (as posted in #6)

  12. #12
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    I hate to bail out on a OP I started, but I can solve this issue by moving the functionality to another form. I have a lot to do and I have to move on. I commented out the troublesome code and I'll leave the tread open in case someone as the desire to continue with ideas.
    Thanks,
    Bill

  13. #13
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    I appreciate you are busy and have fixed this but my statement should have been "There is no filter or where clause in the open report command"
    That is what I would term as a "dynamic filtered" report, not code in the report that does some adjusting.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  14. #14
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    Since I didn't post the report's Open Event code, you didn't see the RecordSource filtering determined based on a couple of the app's global variables. Anyway, at the time I saw your post, among other things I tried was to temporarily create a special RecordSource query for the report that reflected the filtering otherwise determined dynamically and attempted to again execute the OutputTo. I tried that both with and without a DoCmd.OpenReport occurring first but the error persisted.
    Thanks for your follow-on thoughts,
    Bill

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

Similar Threads

  1. Replies: 7
    Last Post: 03-11-2020, 04:30 AM
  2. Replies: 6
    Last Post: 01-13-2015, 04:54 PM
  3. Initialization Module/Class
    By swalsh84 in forum Modules
    Replies: 1
    Last Post: 12-13-2014, 12:25 PM
  4. Replies: 2
    Last Post: 01-07-2013, 07:24 AM
  5. class module vs regular module
    By Madmax in forum Modules
    Replies: 1
    Last Post: 05-01-2012, 03:44 PM

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