How to open report directly from Access db?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • trivanka
    New Member
    • Feb 2013
    • 10

    How to open report directly from Access db?

    How to open report directly in Access 2010 when we open the file?

    Under options - Current Database, I see Display Form.

    Do I need to write a VBA code for this and if so, which event.

    I also want this to open in print preview mode instead of Report.

    TIA
    SFL
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    The code to open the report is
    Code:
    DoCmd.OpenReport "report name", acViewPreview
    The event depends on how you want to open it. If you have a button that you want to click to open the report, then you would put this in the button's OnClick event. I would say that this is probably the most common method, but you can put it in any event that you want as long as you know what triggers the event and you want the report to open every time the event is triggered.

    Comment

    • trivanka
      New Member
      • Feb 2013
      • 10

      #3
      I also noticed that the default view in my report property was set to Report, I changed it to Print Preview and now when I open the report, it opens in print preview.

      But my issue is little different.
      In my access db, I have couple of tables and one report, no form.

      So I want my report to open directly when we double click the access db file, so user dont have to go an extra step of opening and db file and then clicking the report.

      I tried to put your code in open event of report but that did not help.

      Comment

      • Seth Schrock
        Recognized Expert Specialist
        • Dec 2010
        • 2965

        #4
        The OnOpen event of the report only runs when the report gets opened so you can't very well tell the report to open when it is already being opened.

        What you will need to do is create a blank form. Put the code to open the report in the form's OnOpen event and then set Cancel = True. This will close the form after the report is opened. You then need to go to File>Options>Cu rrent Database and then in the Display Form drop down box, select the name of the form that you just created. This will make it so that when you open the database, the form will start to open, this will trigger the form's OnOpen event and open the report. The form will then close leaving you with just the report. The user won't even see that the form was opened.
        Last edited by Seth Schrock; Feb 15 '13, 07:07 PM. Reason: Made it OnOpen event instead of OnLoad

        Comment

        • trivanka
          New Member
          • Feb 2013
          • 10

          #5
          Blank form created and inserted following code
          Code:
          Private Sub Form_Load()
          DoCmd.OpenReport "WFA Testing Status Report", acViewPreview
          Cancel = True
          End Sub
          With the above code, focus is on the Form and it stays open.

          Comment

          • Seth Schrock
            Recognized Expert Specialist
            • Dec 2010
            • 2965

            #6
            Try instead of the Cancel = True try DoCmd.Close acForm, "your blank form name"

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32645

              #7
              The first approach is far preferable. The only problem there is trying to use the Form_Load() event procedure instead of the Form_Open(Cance l As Integer) event procedure, which you will probably see quite easily, is actually designed to allow cancelling, whereas Form_Load() is not.

              PS. I'm afraid I must reset Best Answer here as it is not the sort of information we want to propagate.
              PPS. Seth, if you'd like to update post #4 to correct this confusion then I'll happily set it as Best Answer. To get my attention drop a post in at the end and I'll delete that when I see it.
              Last edited by NeoPa; Feb 15 '13, 06:29 PM. Reason: Added PS.

              Comment

              • zmbd
                Recognized Expert Moderator Expert
                • Mar 2012
                • 5501

                #8
                There is an alternative.
                You do not need the form in the open event


                In V2010 you still have the "AUTOEXEC" macro available.

                This is one of the few times I use the macro language.

                Ribbon:Create:M acros & Code
                Select the Scroll Icon Labeled "Macro"
                When the macro editor opens
                Click the dropdown arrow
                Scroll down to the: OpenReport
                In the options block that appears in the window:
                Report Name: your report
                View: Print Preview
                Filter name: blank
                Where Condition: blank
                Window Mode: Normal

                Save the Macro as "AUTOEXEC"
                Close the database, reopen the database, the report will now open.
                Last edited by zmbd; Feb 17 '13, 12:31 PM. Reason: [Z{Errors and Omissions}]

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32645

                  #9
                  I know that a number of respected experts still use macros for just that reason. My own preference is still to avoid them. This can be done with forms and VBA, so why not do so. That's my thinking anyway. As I say, if you (or anyone) chooses the AutoExec appraoch they will certainly find themselves in good company.

                  Comment

                  Working...