2

I am trying to pull and send a report to a printer. I found this but i get these odd object type errors. I think that I am using this wrong. sorry for the noob question but if i could get some help that would be solid .

private void button13_Click(object sender, EventArgs e)
{

        DoCmd.OpenReport("Sales", //ReportName
                          objAccApp.AcView.acViewPreview, //View
                          System.Reflection.Missing.Value, //FilterName
                          System.Reflection.Missing.Value //WhereCondition);
}

Severity Code Description Project File Line Error CS0103 The name 'DoCmd' does not exist in the current context AllianceERP C:\Users\dhelm.ALLMATINC.001\Documents\Visual Studio 2013\Projects\AllianceERP\AllianceERP\Form1.cs 1033

Severity Code Description Project File Line Error CS0103 The name 'AcView' does not exist in the current context AllianceERP C:\Users\dhelm.ALLMATINC.001\Documents\Visual Studio 2013\Projects\AllianceERP\AllianceERP\Form1.cs 1035

Wesley Lomax
  • 2,067
  • 2
  • 20
  • 34

1 Answers1

2

DoCmd.OpenReport is a VBA method and it can't be used directly in C#.

Try using Access Office Interop Objects:

Access.Application oAccess = null;

// Start a new instance of Access for Automation:
oAccess = new Access.ApplicationClass();

// Open a database in exclusive mode:
oAccess.OpenCurrentDatabase(
   "c:\\mydb.mdb", //filepath
   true //Exclusive
   );

// Preview a report named Sales:
oAccess.DoCmd.OpenReport(
   "Sales", //ReportName
   Access.AcView.acViewPreview, //View
   System.Reflection.Missing.Value, //FilterName
   System.Reflection.Missing.Value //WhereCondition
   );
tezzo
  • 10,858
  • 1
  • 25
  • 48
  • i used your code updated my using Access = Microsoft.Office.Interop.Access; but i get thies error Severity Code Description Project File Line Error CS1752 Interop type 'ApplicationClass' cannot be embedded. Use the applicable interface instead. AllianceERP C:\Users\dhelm.ALLMATINC.001\Documents\Visual Studio 2013\Projects\AllianceERP\AllianceERP\Form1.cs 1040 – AllianceMaterials Oct 12 '15 at 15:32
  • check this: http://stackoverflow.com/questions/2483659/interop-type-cannot-be-embedded – tezzo Oct 12 '15 at 15:37