tags:

views:

31

answers:

1

I'm trying to generate PDF from Access report. Report pop's up dialog "Enter Parameter Value". How I could fill it with C#? Is it even that reportParameter (whereCondition)?

using System;
using MsAccess = Microsoft.Office.Interop.Access;

namespace mdb2pdf
{
    class Program
    {
        public static void Main(string[] args)
        {
            string MDBFile = @"d:\example.mdb";
            string PDFFile = @"d:\example.pdf";
            string reportName = @"myReport";
            string reportParameter = @"[Name?] LIKE 'myName'";

            MsAccess.Application app = new MsAccess.Application();

            app.OpenCurrentDatabase(MDBFile, false, "");
            app.DoCmd.OpenReport(reportName, Microsoft.Office.Interop.Access.AcView.acViewPreview, Type.Missing, reportParameter, MsAccess.AcWindowMode.acWindowNormal, Type.Missing);

            app.DoCmd.OutputTo(MsAccess.AcOutputObjectType.acOutputReport, reportName, MsAccess.Constants.acFormatPDF, PDFFile, Type.Missing, Type.Missing, Type.Missing);
            app.CloseCurrentDatabase();
        }
    }
}
A: 

You could try changing the SQL behind the query the report is based on to include your criteria at runtime. Not sure how to do it through c# but this is how you could do it in native VBA so hopefully you can reverse engineer it from there

DBEngine(0)(0).QueryDefs("Query_report_is_based_on").SQL = “SELECT foo FROM tblBar WHERE foo=’Something’ ”
Kevin Ross
Why not just pass the criteria argument to the OpenReport and not worrying about parameterizing the SQL at all?
David-W-Fenton