1

How would I change a Microsoft Access report's RecordSource property from a C# Windows Forms handler, using Office.Interop code?

I need to silently save the report as a PDF, using AccessApp.DoCmd.OutputTo(...) without any Parameter prompts from Access. So I want to temporarily change RecordSource to an query defined by the form's code, silently save the PDF report, and then change RecordSource back to a default.

I've tried something like AccessApp.Reports["Report Name"].RecordSource but I get errors!

Is there a better way to do this than the way I suggested?

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Warwick
  • 925
  • 1
  • 10
  • 22

1 Answers1

1

The following works for me (tested with Access 2010):

accessApp.DoCmd.OpenReport(
        "Report1", 
        Microsoft.Office.Interop.Access.AcView.acViewDesign);

Microsoft.Office.Interop.Access.Report rpt = accessApp.Reports["Report1"];
rpt.RecordSource = "SELECT * FROM Clients WHERE ID<=3";

accessApp.DoCmd.OutputTo(
        Microsoft.Office.Interop.Access.AcOutputObjectType.acOutputReport,
        "",
        "PDF Format (*.pdf)",
        @"C:\Users\Gord\Desktop\zzzTest.pdf",
        false,
        null,
        null,
        Microsoft.Office.Interop.Access.AcExportQuality.acExportQualityPrint);

// now close the report without saving the change to the RecordSource property
accessApp.DoCmd.Close(
        Microsoft.Office.Interop.Access.AcObjectType.acReport,
        "Report1",
        Microsoft.Office.Interop.Access.AcCloseSave.acSaveNo);
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418