Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / April 2005

Tip: Looking for answers? Try searching our database.

Access Interop - List/Edit reports

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
STech - 25 Apr 2005 16:53 GMT
How can I using interop (C# or VB.net)

1) Get a list of reports in an access database
2) Programatically change the "Control Source" of a report. ie. perform a
search and replace - eg. CustName becomes CustomerName.

I know how to get a list of tables (dao.TableDef) and queries (dao.QueryDef)
using interop. The reports collection count returns 0 so I am not able to
enmerate it.

app = new ApplicationClass();
app.OpenCurrentDatabase(databasePath, true, "");
db = app.CurrentDb();

app.Reports.Count <- returns 0

Thanks.
Steven Cheng[MSFT] - 26 Apr 2005 08:29 GMT
Hi Stech,

Welcome to MSDN newsgroup.
From your description ,you 're going to use Office Access Automation in a
.net application so as to manipulate the Reports in a certain Access
database, yes?

As for your first question : "get a list of reports in an access database",
based on my research, we can't directly use the AccessApplication object's
Reports collection, but reference the Applicaction object's CurrentProject
first , then use the Project's  "AllReports" property to access all the
existing reports in that access project. For example:
=======================
[VB.NET]
.......
oAccess.OpenCurrentDatabase(strPath, False)

Dim objs As Access.AllObjects

     
objs = oAccess.CurrentProject.AllReports

Dim iter As System.Collections.IEnumerator

iter = objs.GetEnumerator()

While iter.MoveNext()

If iter.Current.Name = "Network - Summary" Then

Dim obj As Access.AccessObject

obj = iter.Current

MessageBox.Show(obj.FullName)

Marshal.ReleaseComObject(obj)
obj = Nothing
             
End If

End While

Marshal.ReleaseComObject(objs)
objs = Nothing
===========================

As for the "Control Source"  you mentioned in the second question, I'm not
sure whether you mean the ControlSource propety of a certain Textbox
control in the report's designview or ....   Would you provide some further
desciption ?  

Thanks,

Steven Cheng
Microsoft Online Support

Signature

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

STech - 26 Apr 2005 18:02 GMT
Thanks for the reply Steven. What I am trying to do is enmerate through the
reports in an access database, look for references to specific column names
and replace that with a different column name. I have already accomplished
this for the queries but don't know how to do it with reports.

so example: if a report has a reference to say CustName in table Customers,
I want to replace that reference with CustomerName in table Customers2.

Thansk for looking into this.

> Hi Stech,
>
[quoted text clipped - 56 lines]
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
"Peter Huang" [MSFT] - 27 Apr 2005 08:47 GMT
Hi

To get the report referecne, we need to open the report first and then
iterate through the controls to change its controlsource property.

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
       Dim oAccess As New Access.Application
       Dim rpt As Access.Report
       oAccess.Visible = False
       oAccess.OpenCurrentDatabase("c:\test.mdb")
       oAccess.DoCmd.OpenReport("Customer Labels",
Access.AcView.acViewDesign)
       rpt = oAccess.Reports("Customer Labels")
       Dim cnt As Access.Control
       For Each cnt In rpt.Controls
           Debug.WriteLine(cnt.ControlName)
           Debug.WriteLine(cnt.ControlSource)
           cnt.controlSource = "=[Test]"
       Next
   End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

STech - 27 Apr 2005 17:00 GMT
Peter,

Thanks for the reply. I am writing this in C# and ran into two problems:

1) I am writing this in C# - Access.Control does not have properties called
ControlName and ControlSource. I get the following error:

'Microsoft.Office.Interop.Access.Control' does not contain a definition for
'ControlSource'

2) Some controls may not necessarily have a ControlSource defined so there
needs to be a way to check if a control has this defined.

Thanks in advance.

> Hi
>
[quoted text clipped - 25 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 28 Apr 2005 04:35 GMT
Hi

Here is the C# version.
We can consider the Access.Control as the base class of the control on
report. e.g. the Textbox. Because  VB.NET support latebinding implicitly,
we can call the property on the Access.Control directly. In C#, we have to
cast it to the correct type first, or we have to use reflection to do the
latebinding in C# as below.

As for the control that did not support controlsource, we have to use the
try...catch block to handle the issue.

        private void button1_Click(object sender, System.EventArgs e)
        {
            Access.Application oAccess = new Access.ApplicationClass();
            object oMissing = System.Reflection.Missing.Value;
            oAccess.OpenCurrentDatabase(@"c:\Northwind.mdb",true,String.Empty);
            oAccess.DoCmd.OpenReport("Customer Labels", Access.AcView.acViewDesign,
                oMissing,oMissing,Access.AcWindowMode.acHidden,oMissing);
            Access.Report rpt=null;
            foreach(Access.Report o in oAccess.Reports)
            {
                if (o.Name == "Customer Labels")
                {
                    rpt = o;
                    break;
                }
            }
       
            foreach (Access.Control cnt in rpt.Controls)
            {
                try
                {
                   
System.Diagnostics.Debug.WriteLine(cnt.GetType().InvokeMember("ControlName",
System.Reflection.BindingFlags.GetProperty,null,cnt,null));
                   
System.Diagnostics.Debug.WriteLine(cnt.GetType().InvokeMember("ControlSource
",System.Reflection.BindingFlags.GetProperty,null,cnt,null));
                   
System.Diagnostics.Debug.WriteLine(cnt.GetType().InvokeMember("ControlSource
",System.Reflection.BindingFlags.SetProperty,null,cnt,new
object[]{"Test"}));
                }
                catch{}
            }
        }
Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

STech - 29 Apr 2005 04:35 GMT
Perfect Peter! Thanks a lot!

> Hi
>
[quoted text clipped - 50 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 29 Apr 2005 06:58 GMT
Hi

Cheers!

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.