none
Viewing and Printing Access Reports in C# RRS feed

  • Question

  • My code is as follows:

                    var objAccess = new Microsoft.Office.Interop.Access.Application();
                    objAccess.Visible = false;
                    objAccess.OpenCurrentDatabase(@"M:\Workgrp\AccessApplications\Minor Maintenance Tracker V3.2\BackUp\Minor       Maintenance Tracker_be.accdb");
                    objAccess.DoCmd.SetParameter("@Year", comboBox3.Text);
                    objAccess.DoCmd.OpenReport(formName, Microsoft.Office.Interop.Access.AcView.acViewPreview);

    To view and print the report I have added:

    objAccess.DoCmd.PrintOut();

    Is there a more elegant way of achieving this?

    Also, I am trying to figure out how I can close the Access database after displaying the report.

    If I do objAccess.DoCmd.CloseDatabase() it would close the report and the database.

    Wednesday, August 28, 2019 6:06 PM

All replies

  • See the following

    https://docs.microsoft.com/en-us/office/troubleshoot/office-developer/automate-access-using-visual-c#print-or-preview-an-access-report

    // Open the report in preview mode using a WhereCondition:
    oAccess.DoCmd.OpenReport(
       "Employees", //ReportName
       Access.AcView.acViewPreview, //View
       System.Reflection.Missing.Value, //FilterName
       "[EmployeeID]=1" //WhereCondition
       );
    
    // Print 2 copies of the active object: 
    oAccess.DoCmd.PrintOut(
       Access.AcPrintRange.acPrintAll, //PrintRange
       System.Reflection.Missing.Value, //PageFrom
       System.Reflection.Missing.Value, //PageTo
       Access.AcPrintQuality.acHigh, //PrintQuality
       2, //Copies
       false //CollateCopies
       );
    
    // Close the report preview window: 
    oAccess.DoCmd.Close(
       Access.AcObjectType.acReport, //ObjectType
       "Employees", //ObjectName
       Access.AcCloseSave.acSaveNo //Save
       );


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, August 28, 2019 6:26 PM
    Moderator