none
Open Access database from C# RRS feed

  • Question

  • IM working on a small C# program to backup My Access database automatically and woould like to add a few bells and whistles. Id like to be able to open the selected database from the program in exclusive and non-exclusive mode. Ive read the articl on SMSDN about Access automation as well as others but still have a problem.
    using Access = Microsoft.Office.Interop.Access; 
     
    ....... 
     
    private void btnOpenCurrDB_Click(object sender, EventArgs e) 
       // Start a new instance of Access for Automation: 
       Access.Application oAccess = new Access.ApplicationClass(); 
       if (cbExclMode.Checked == true) 
       { 
           //Open a database in exclusive mode: 
           oAccess.OpenCurrentDatabase(@"C:\FTTH.mdb", true,null); 
       } 
       else 
       { 
           // Open a database in non-exclusive mode: 
           oAccess.OpenCurrentDatabase(@"C:\FTTH.mdb", false,null); 
       } 
    When I run the program, and click on the OpenCurrDB button I get 'InvalidCastException was unhandled'
    Unable to cast COM object of type 'Microsoft.Office.Interop.Access.ApplicationClass' to interface type 'Microsoft.Office.Interop.Access._ApplicationClass'. The exception cmoes in either of the two oAccess.OpenCurrentDatabase lines depending on whether or not I have the exclusive checkbox checked.

    Im a novice programmer, the rest of my program works fine, I just cant seem to get my hand on this part.
    Friday, September 12, 2008 9:51 PM

Answers