tags:

views:

79

answers:

3

How to enumerate MS Access reports in windows dot net application.

I have a listbox that should show the available reports in a MS Access db. Tried following code, but it did not show anything.

oAccess = GetAccessDBObject(DBLocation);                              
Reports accessReports = oAccess.Reports; // oAccess.Reports.Count = 0!!! No data comes out here!!!

How do I iterate ms-access reports in .net?

A: 
foreach ( Reports rprt in oAccess.Reports )
{

 listbox.items.Add(rprt);
}

// Disclaimer: Wrote it off the top of my head, don't know if a Reports object has a name property to show it's name in your listbox.

Tony
According to the docs, this will only give you Reports that are already open; see my answer how to get a collection of every report.
Doc Brown
+2  A: 

I tried this example in C# and it works:

        Access.Application oAccess = new Access.Application();
        oAccess.OpenCurrentDatabase(@"D:\tmp\dbtest.mdb", false, "");
        Access.AllObjects reports = oAccess.CurrentProject.AllReports;
        Console.WriteLine(reports.Count);
        foreach (Access.AccessObject report in reports)
        {
            Console.WriteLine(report.Name);
        }

(you have to add a reference to Microsoft Access Object Library to your project, of course.)

Doc Brown
Here you can't type cast to 'Reports' class!! How to overcome?
Dhana
Edited answer according to your question.
Doc Brown
Note that this solution requires that Access be installed on the system.
Tony Toews
Hi, how to get data from 'reports' object here?
Dhana
+1  A: 

As Tony Toews mentions, if Access is not installed, you can't automate. This SQL will work on any system:

  SELECT MSysObjects.Name
  FROM MSysObjects
  WHERE (((MSysObjects.Type)=-32764))
  ORDER BY MSysObjects.Name;

However, note that permissions can get in the way of accessing the Jet/ACE system tables.

Also, there's not much you can do with a list of Access reports without Access! But I thought it was good for completeness to mention this.

David-W-Fenton