How to switch back from report Print Preview to Report View via VBA ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yousaf Shah
    New Member
    • Feb 2011
    • 25

    How to switch back from report Print Preview to Report View via VBA ?

    Hello everybody.
    I am developing database for my patients. I have almost the needed stuff but now struck at report level. At the end of all required data entry, I want to give prescription to my patients in printed form. For that I have made a report that opens in 'Report view' as a pop-up window. On that report, name it as 'rptOPDTreatmen t', I have put a command button 'cmdPreview' that I use to open report in 'Print Preview'. Now I want to go back to 'Report View', I have to right click on report border and select 'report View' to go back. Is there any vba code that I could place in 'OnClose' event that could reopen the report in 'Report View'..?
    I have used following code in OnClose event

    Code:
    Private Sub Report_Close()
    Dim HisttID, rptView As Integer
    HisttID = Me.HistoryID
    rptView = Report.CurrentView
    If (rptView = 5) Then
     DoCmd.OpenReport "rptOPDTreatment", acViewReport, , "HistoryID = " & HisttID
    End If
    End Sub
    But it gives run time error '2585' 'This action can not be carried out while processing a form or report event.

    I have tried TempVars.Add method to fetch the CurrentView Integer in OnLoad event but that did not work either.
    Please help me solve this issue.
    Thanks
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    I know that ACC2007/2010 have made reports interactive. IMHO: A very annoying and useless thing to do. You have; however, found out the hard way that reports really shouldn't be interactive at all in that once opened... how do you get back.

    If you will tell me which version of ACC you are using I may be able to help; however, it's not a promising thing.

    Comment

    • Yousaf Shah
      New Member
      • Feb 2011
      • 25

      #3
      Thank zmbd for response.
      I am using MS ACCESS 2010. I get back by right clicking on report border and then selecting report view.

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        Ok,
        THis isn't pretty but it works.
        Open the report in design view
        show the properties for your report
        Event tab
        on close event

        Code:
        Option Compare Database
        Option Explicit
        
        Private Sub Report_Close()
            On Error Resume Next
            DoCmd.RunCommand acCmdReportView
        End Sub
        When you close out of the print preview it should take you back to the report view.
        I hate the use of the resume next error trap; however, in this case it seems to be the only solution.

        See if this works for you...

        If I come across somethning better I'll update it here.

        -z

        Comment

        • Yousaf Shah
          New Member
          • Feb 2011
          • 25

          #5
          Thanks zmbd
          I have found another simpler way. That if you press 'c' while in 'Print Preview', it takes you to 'Report View'.
          Anyways thanks very much for your concern.

          Comment

          • zmbd
            Recognized Expert Moderator Expert
            • Mar 2012
            • 5501

            #6
            Glad that works for you; however, that does not work for me in either of the test databases I've currently open.
            Thus, there must be something else going on... curious!

            Comment

            Working...