469,267 Members | 1,055 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

Opening Mdb bypassing AutoExec with C# Solved, but curious about why solution works

112 Expert 100+

I have been messing around with automating Access (I am attempting to actually manipulate the Access objects themselves so it requires going beyond data access) from a c# program (long story as to why, but any how...). I am dealing with databases that all have AutoExec macros within them which automatically run when the database opens, which I don't want the database to do. In Access this is bypassed by holding down the shift key. I have attempted to replicate doing this when opening from the C# program, in so doing I ported a piece of code over from VBA which did this very thing, using the SetKeyboardState API call. After monkeying around with the code a lot I finally arrived at this solution which does what I want, but I am not sure why:
Expand|Select|Wrap|Line Numbers
  1.    class OpenMdb
  2.     {
  3.        private const int VK_SHIFT = 0x10;
  4.         [DllImport("user32")]
  5.             public static extern long SetKeyboardState(ref byte lppbKeyStat);
  6.         [DllImport("user32")]
  7.              public static extern long SetFocus(long hwnd);
  8.         public static Access.Application NoAutoexec (int hWnd, string path, bool visible)
  9.         {
  10.             Access.Application app;
  11.             byte[] abytCodesSrc = new byte[256];
  12.             byte[] abytCodesDest = new byte[256];
  13.             app = new Access.Application();
  14.             app.Visible = visible;
  15.             abytCodesDest[VK_SHIFT] = 128;
  16.             SetKeyboardState(ref abytCodesDest[0]);
  17.             System.Windows.Forms.SendKeys.Send("+");
  18.             app.OpenCurrentDatabase(path, true, "");
  19.             SetFocus(hWnd);
  20.             return app;
  21.         } 
  22.     }
The thing that has me puzzled is that the key to making this work was inserting the SendKeys statement. Sendkeys alone won't make this happen. The SetKeyboardState alone won't do what I am asking, but this seems to work. I think that I probably have a fundamental misunderstanding of what the SetKeyboardState API is doing. I'd like to figure out why the conjunction of the API and Sendkeys is behaving this way. Any ideas?
Aug 19 '08 #1
0 898

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by PM | last post: by
6 posts views Thread by MLH | last post: by
3 posts views Thread by Hank | last post: by
28 posts views Thread by Madhur | last post: by
6 posts views Thread by darren via AccessMonster.com | last post: by
10 posts views Thread by Tim Streater | last post: by
6 posts views Thread by lyle fairfield | last post: by
8 posts views Thread by Rex the Strange | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.