Access MSAccess Properties without actually opening the Database

2.1k Views Asked by At

What i am trying is to access the MS Access properties without actually opening the database.

Here is some code to get a better understanding:

var processStartInfo = new ProcessStartInfo(args[0]) 
    { 
        WindowStyle = ProcessWindowStyle.Hidden, 
        CreateNoWindow = true
    };

Process.Start(processStartInfo);

application = (Access.Application)Marshal.GetActiveObject("Access.Application");

dao.Property allowByPassKeyProperty = null;

foreach (dao.Property property in application.CurrentDb().Properties)
{
    if (property.Name == "AllowByPassKey")
    {
        allowByPassKeyProperty = property;
        break;
    }
}

My problem is that in that case, I open the database in order to look for the properties (application.CurrentDb().Properties) and the MS Access startup stuff kicks in.

I want to avoid all the startup stuff, and just inject the right value for the property.

Is it possible to go through the properties, maybe with reflection and late binding like that: http://www.codeproject.com/KB/database/mdbcompact_latebind.aspx?

Or is there any other option to achieve what I'd like?

2

There are 2 best solutions below

1
On BEST ANSWER

Sorry I don't have details, but looking into using DAO to open the Access database(assuming it's pre-2007). DAO is the native access/jet code, so you don't have to actually start the entire Access application.

Sone old VB.Net (yes, .Net) code I have lying around:

m_oEngine = New DAO.DBEngine

m_oEngine.SystemDB = sWorkgroupFile

m_oWorkspace = m_oEngine.CreateWorkspace("My Workspace", sUserName, sPassword, DAO.WorkspaceTypeEnum.dbUseJet)

m_oDatabase = m_oWorkspace.OpenDatabase(sDatabaseFile, bExclusive, bReadOnly)  ' Note DAO docs say the second parameter is for Exclusive
0
On

Just in case anyone is working with MS Access and needs the same program, here is the code.

The program can toggle the AllowBypassKey Property in MS Access *.mdb, *.mde files. Tested only with MS Access 2003. It can be extremely usefull, if you have deactivated the property, and your AutoExec Macro is going wild.

namespace ToggleAllowBypassKey
{
    public class Program
    {
        private static DAO.DBEngine dbEngine;

        private static DAO.Database database;

        public static void Main(string[] args)
        {
            try
            {
                if (args.Length == 0)
                {
                    Console.WriteLine("Please enter an MS Access application path as a parameter!");
                    return;
                }

                dbEngine = new DAO.DBEngine();
                database = dbEngine.OpenDatabase(args[0]);

                DAO.Property allowBypassKeyProperty = null;

                foreach (dao.Property property in database.Properties)
                {
                    if (property.Name == "AllowBypassKey")
                    {
                        allowBypassKeyProperty = property;
                        break;
                    }
                }

                if (allowBypassKeyProperty == null)
                {
                    allowBypassKeyProperty = database.CreateProperty("AllowBypassKey", DAO.DataTypeEnum.dbBoolean, false, true);
                    database.Properties.Append(allowBypassKeyProperty);
                    Console.WriteLine("AllowBypassKey Property has been added. It's Value is '" + allowBypassKeyProperty.Value + "'");
                }
                else
                {
                    allowBypassKeyProperty.Value = !allowBypassKeyProperty.Value;
                    Console.WriteLine("AllowBypassKey set to '" + allowBypassKeyProperty.Value + "'!");
                }
            }
            catch(Exception exception)
            {
                Console.WriteLine("Exception Message: " + exception.Message);
                Console.WriteLine("Inner Exception:" + exception.InnerException);
            }
            finally
            {
                database.Close();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(database);
                database = null;

                System.Runtime.InteropServices.Marshal.ReleaseComObject(dbEngine);
                dbEngine = null;

                Console.WriteLine();
                Console.WriteLine("Press enter to continue ...");
                Console.ReadLine();
            }
        }
    }
}