Printing Microsoft Access 2010 Reports from Visual Studio


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame


The world of programming is a strange realm of uncertainties. Sometimes a need arises to do a task because "sloppy programmers" didn't consider all the facts, or "sloppy planning". I put these terms into quotation marks because these reasons remain open for discussion. Yes, there are times that these reasons are the main reason for problems in your code and programs, but not always. Sometimes it depends on the situation at hand and the resources at hand. As a programmer, you should be willing to take these challenges head-on and face them.

The reason I mention these facts is because today's program is one of those topics that usually gets frowned upon - which is a tad unfair. I will demonstrate with my article today, how to print a Microsoft Access 2010 report from within your C# or VB.NET program. This topic is usually the start of a discussion on decent reporting tools such as Crystal Reports etc. Face it, not every company will use Crystal Reports or another product, simply because of funding or hardware concerns. Enough rambling, Let's get started with our project...


Our design is simple, as illustrated in Figure 1

Our design
Figure 1 - Our design


Add a project Reference to Microsoft Access 14 Object Library. By clicking on Project, Add reference, select the COM tab, and scroll down to Microsoft Access Object library. Just a note, you could ( if you do not have Microsoft Office 2010 yet ) make use of the Microsoft Access 12 Object library for Office 2007. There should be no differences. With this project, I tested with both Microsoft Access 2007 and Microsoft Access 2010


Apart from adding the Project Reference to Microsoft Access, we need to be able to reference the Access code library through code as well, add the following namespace:


Imports Microsoft.Office.Interop.Access 'Import MS Access funtionalities, ADD PROJECT REFERENCE AS WELL


using objAccApp = Microsoft.Office.Interop.Access; //Import MS Access funtionalities, ADD PROJECT REFERENCE AS WELL

Already the succinct differences between VB.NET and C# is clearly visible. VB.NET follows a three step process here:

  1. VB.NET imports the Access library
  2. It creates an object from that library (which we will do next)
  3. Lastly, it instantiates that object so that we could use it

C# on the other doesn't work like that. With C# we directly make an object of the Access library, and then instantiate other objects from it.

Browse Button

Add the following code for your browse button:


    Private Sub btnARBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnARBrowse.Click

        Dim objAccApp As New Microsoft.Office.Interop.Access.Application() 'Instantiate Access Object

        objAccApp.Visible = False 'Do Not Display Access Window

        Dim strFileName As String = txtARSource.Text.Trim() 'File Name To Open

        Dim ofdAccReport As New OpenFileDialog() 'Create New Open File Dialog

        ofdAccReport.Filter = "Microsoft Access (*.accdb)|*.accdb)" 'Set File Filter For OFD

        If strFileName.Length > 0 Then 'If File Selected

            ofdAccReport.FileName = strFileName

        End If

        If ofdAccReport.ShowDialog() = DialogResult.Cancel Then 'If Cancelled


        End If

        txtARSource.Text = ofdAccReport.FileName 'Display File Name 

        lstARReports.Items.Clear() 'Erase Previous ListBox Items

        objAccApp.OpenCurrentDatabase(ofdAccReport.FileName, False, "") 'Open Selected Access Database

        If Not objAccApp.Visible = False Then 'Do Not Display access Window(s)

            objAccApp.Visible = False

        End If

        Dim strReportSQL As String = "SELECT [Name] FROM MSysObjects WHERE Type = -32764" 'Search All Access Reports

        Dim daoDB As dao.Database = objAccApp.CurrentDb() 'Open The Access Database

        Dim rsReports As dao.Recordset = daoDB.OpenRecordset(strReportSQL, Type.Missing, Type.Missing, Type.Missing) 'Find All Reports

        While Not rsReports.EOF

            lstARReports.Items.Add(rsReports.Fields(0).Value) 'Add Each Report Name To The ListBox

            rsReports.MoveNext() 'Continue

        End While

        rsReports.Close() 'Close All Objects



        rsReports = Nothing 'Release All Resources
        daoDB = Nothing
        objAccApp = Nothing

    End Sub


        private void btnARBrowse_Click(object sender, EventArgs e)
            string strFileName = txtARSource.Text.Trim(); //File Name To Open

            OpenFileDialog ofdAccReport = new OpenFileDialog(); //Create New Open File Dialog

            ofdAccReport.Filter = "Microsoft Access (*.accdb)|*.accdb)"; //Set File Filter For OFD

            if (strFileName.Length > 0) //If File Selected
                ofdAccReport.FileName = strFileName;


            if (ofdAccReport.ShowDialog() == DialogResult.Cancel) //If Cancelled


            txtARSource.Text = ofdAccReport.FileName; //Display File Name

            lstARReports.Items.Clear(); //Erase Previous ListBox Items

            objAccApp.Application app = new objAccApp.Application(); //Instantiate Access Object

            app.Visible = false; //Do Not Display Access Window

            app.OpenCurrentDatabase(ofdAccReport.FileName, false, ""); //Open Selected Access Database

            string strReportSQL = "SELECT [Name] FROM MSysObjects WHERE Type = -32764"; //Search All Access Reports

            dao.Database daoDB = app.CurrentDb(); //Open The Access Database

            dao.Recordset rsReports = daoDB.OpenRecordset(strReportSQL, Type.Missing, Type.Missing, Type.Missing); //Find All Reports

            while (!rsReports.EOF)

                lstARReports.Items.Add(rsReports.Fields[0].Value); //Add Each Report Name To The ListBox

                rsReports.MoveNext(); //Continue


            rsReports.Close(); //Close All Objects

            rsReports = null; //Release All Resources
            daoDB = null;
            app = null;


In this procedure we create a new OpenFileDialog and allow for file selection. Once a file has been selected, we place the name into our Source textbox. Now it gets interesting. To open the physical Microsoft Access file we do not make use of ADO.NET here, as we are making use of the Microsoft Access object Library's functionalities. We make use of the OpenCurrentDatabase method (inside the Access object library) to open the selected database.

To identify the report objects present in the Access database, we use an SQL statement searching through MSysObjects for type -32764 (which is the report objects). Inside a loop that loops through all the objects present in the database, we place each report name into a listbox.

Lastly, we just close all the Access objects and release all the memory resources.

If you were to run your project now, you'd be able to get a list of all reports inside your selected database.

Print Button

Add the following code behind your Print button:


    Private Sub btnARPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnARPrint.Click

        Dim objAccApp As New Microsoft.Office.Interop.Access.Application() 'Instantiate Access Application Object

        Dim strAccReport As String = lstARReports.SelectedItem.ToString() 'Get Selected ListBox Item

        objAccApp.OpenCurrentDatabase(txtARSource.Text.Trim(), False, "") 'Open Database

        If Not objAccApp.Visible = False Then 'Do Not Show Access Window(s)

            objAccApp.Visible = False

        End If

        objAccApp.Visible = False

        objAccApp.DoCmd.OpenReport(strAccReport, Microsoft.Office.Interop.Access.AcView.acViewPreview, Type.Missing, Type.Missing, AcWindowMode.acWindowNormal, Type.Missing) 'Open Selected Report

        objAccApp.DoCmd.PrintOut(AcPrintRange.acPrintAll, Type.Missing, Type.Missing, AcPrintQuality.acHigh, Type.Missing, Type.Missing) 'Print Report

        objAccApp.CloseCurrentDatabase() 'Close Database

        objAccApp = Nothing 'Release Resources

    End Sub


        private void btnARPrint_Click(object sender, EventArgs e)

            string strAccReport = lstARReports.SelectedItem.ToString(); //Get Selected ListBox Item

            objAccApp.Application app = new objAccApp.Application(); //Instantiate Access Application Object

            app.OpenCurrentDatabase(txtARSource.Text.Trim(), false, ""); //Open Database

            app.Visible = false; //Do Not Show Access Window(s)

            app.DoCmd.OpenReport(strAccReport, Microsoft.Office.Interop.Access.AcView.acViewPreview, Type.Missing, Type.Missing, objAccApp.AcWindowMode.acWindowNormal, Type.Missing); //Open Selected Report

            app.DoCmd.PrintOut(objAccApp.AcPrintRange.acPrintAll, Type.Missing, Type.Missing, objAccApp.AcPrintQuality.acHigh, Type.Missing, Type.Missing); //Print Report

            app.CloseCurrentDatabase(); //Close Database

            app = null; //Release Resources


We obtain the selected (in the list) report name and store it inside a variable named strAccReport. We again make use of the OpenCurrentDatabase method to open the selected database. Now, we make use of the DoCmd method to Open the selected report, and to Print the the report out on paper.

Not too complicated, hey? No.

If you were to run your application now, you'd be able to print out any selected report from your Access database. I am including both sample projects below, just in case you put a foot wrong somewhere.


There, I hope you enjoyed this article and learned from it. This just shows how much is actually possible in the world of programming. Until next time, cheers!

Related Articles



  • Call Access report with parameter

    Posted by Zivko Knezevic on 12/28/2017 01:25pm

    Thanks a lot. You are genious. It works also with older .mdb versions. I have report with parameter. How can I pass the parameter and avoid Access input window? Any help would be appreciated. Thanks!

  • Very good example

    Posted by Jorge Par on 05/15/2016 04:31pm

    Thanks for the example, following the code saves a lot of research time. The only problem is that the code is using DAO which applies to MsAccess 2007. Are you going to revise this for future viewers? Thanks anyway.

  • Thank you

    Posted by Akhtar on 03/30/2016 06:12am

    I read your article it is amasing for me because I was searh many time for my problem to print report of Access 2010 now i can do this by the help of your codd. Thank you very much

  • Student

    Posted by Nancy on 01/24/2014 10:27pm

    Both in the downloaded version for C# and in my code, there is an error on: dao.Database daoDB = app.CurrentDB(); and dao.Recordset rsReports = daoDB.OpenRecordset(startReportSQL, Type.Missing, Type.Missing, Type.Missing): Error: The type or namespace name 'dao' could not be found (are you missing a using directive or an assembly reference?) Any help would be appreciated, thanks!

    • dao is not in Lower Case

      Posted by Ldjiba on 12/06/2016 12:49pm

      Hi Nancy, I must put dao in upcase [DAO].

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date