Results 1 to 2 of 2

Thread: Open Access document with C#?

  1. #1

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    202

    Open Access document with C#?

    I have this class in C# which i developed from examples:

    Code:
    class ManagedAccess:IDisposable
        {
            dynamic _access;
            dynamic _doc;
            bool _isDisposed = false;
    
            public ManagedAccess(string filePath = "")
            {
                _access = new Microsoft.Office.Interop.Access.Application();
                _access.Visible = true;
    
                if (filePath.Equals(string.Empty))
                {
                    _doc = _access.Workbooks.Add();
                    _doc.Activate();
                }
                else
                {
                    _doc = _access.Workbooks.Open(filePath);
                }
            }
    
            protected virtual void Dispose(bool isDisposing)
            {
                if (this._isDisposed)
                    return;
    
                if (isDisposing)
                {
                    this._access.Quit();
                }
    
                // Release unmanaged resources here
                //if (this._excel != null)
                //{
                //    System.Runtime.InteropServices.Marshal.ReleaseComObject(this._excel);
                //}
    
                // Indicate that the object has been disposed.
                this._isDisposed = true;
            }
    
            ~ManagedAccess()
            {
                this.Dispose(false);
            }
    
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
        }
    If you check the constructor there is the word "Workbooks" which suits when opening Excel application from C#.

    Though Access usage will decrease, we still use old Access software and lot of apps are built with it, so I might need to open an Access program from C#.

    What should i replace "Workbooks" with in the following code:

    Code:
          if (filePath.Equals(string.Empty))
                {
                    _doc = _access.Workbooks.Add();
                    _doc.Activate();
                }
                else
                {
                    _doc = _access.Workbooks.Open(filePath);
                }
    This is using Microsoft office Interop libraries and the IDisposable pattern in C#

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Open Access document with C#?

    This should help if you need to open an access database from c# code

    http://www.vbforums.com/showthread.p...73#post5346573
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width