c# Late Binding to MS Acesss Report

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davidmirwis
    New Member
    • Dec 2008
    • 3

    c# Late Binding to MS Acesss Report

    How can I late bind to an MS Access Report from c#
    This is how far I have gotten so far
    I dont know how to do the open report call
    Thanks
    David
    Code:
    string message = "Failed to create Access application.";
    Type accessType = Type.GetTypeFromProgID("Access.Application");
           if (accessType == null)
             {
                throw new Exception(message);
             }
    accessApplication = Activator.CreateInstance(accessType);
    
     string fileName = "C:\\Report.accdb";
    
     accessApplication.GetType().InvokeMember(
                       "OpenCurrentDatabase",
                       System.Reflection.BindingFlags.Default |     System.Reflection.BindingFlags.InvokeMethod,
                    null,
                    accessApplication,
                            new Object[] { fileName  });
  • anijos
    New Member
    • Nov 2008
    • 52

    #2
    Try

    DoCmd.OpenRepor t(
    "Name", //ReportName
    Access.AcView.a cViewPreview, //View
    System.Reflecti on.Missing.Valu e, //FilterName
    System.Reflecti on.Missing.Valu e //WhereCondition
    );

    Regards,
    AniJos

    Comment

    • davidmirwis
      New Member
      • Dec 2008
      • 3

      #3
      AniJos
      Thanks
      That code is for early binding when one uses
      using Access = Microsoft.Offic e.Interop.Acces s;

      I would like to use late binding to pick up the MS Access version of the machine the program is running on

      Regards
      David

      Comment

      • leddy
        New Member
        • Jan 2008
        • 4

        #4
        hi david,

        did you find a solution to this?

        cheers
        leddy

        Comment

        • davidmirwis
          New Member
          • Dec 2008
          • 3

          #5
          c# Late Binding to MS Acesss Report

          No I did not get a solution for this

          Comment

          • leddy
            New Member
            • Jan 2008
            • 4

            #6
            C# and Access using Late Binding

            This is how I've done it if anyone's interested.....

            I have a function in the db called 'printReports' which is where the report is run, it takes three parameters, string dbfFileName, string dbfFilePath and string roundListName.

            Code:
                        object accessObject = null;
                        object[] Parameters;
            
                        string dbfFilePath = Path.GetDirectoryName(sf.Filename);
                        string roundListName = getTXT(dbfFilePath);
                        roundListName = roundListName.Split(',')[1];
                        roundListName = roundListName.ToUpper();
                        string dbfFileName = Path.GetFileNameWithoutExtension(sf.Filename) + ".dbf";
            
                        try
                        {
                            accessObject = Activator.CreateInstance(Type.GetTypeFromProgID("Access.Application"));
            
                            accessObject.GetType().InvokeMember(
                               "OpenCurrentDatabase",
                               BindingFlags.Default | BindingFlags.InvokeMethod,
                               null,
                               accessObject,
                               new Object[] { @"S:\DATA\SMART2\Data\SMART2_DB.mdb" });
            
                            accessObject.GetType().InvokeMember(
                               "Run",
                               System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod,
                               null,
                               accessObject,
                               new Object[] { "printReports", dbfFilePath, dbfFileName, roundListName });
            
                            accessObject.GetType().InvokeMember(
                               "CloseCurrentDatabase",
                               System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod,
                               null,
                               accessObject,
                               null);
            
                        }
                        catch (Exception ex)
                        {
                            string message = ex.Message;
                            while (ex.InnerException != null)
                            {
                                ex = ex.InnerException;
                                message += "\r\n----\r\n" + ex.Message;
                            }
                            MessageBox.Show(message);
                        }
                        finally
                        {
                            if (accessObject != null)
                            {
                                System.Runtime.InteropServices.Marshal.ReleaseComObject(accessObject);
                                accessObject = null;
                            }
                        }
            HTH

            leddy

            Comment

            Working...