473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

112 Expert 100+
Hi,

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.     }
  23.  
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 1025

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

Similar topics

5
by: PM | last post by:
Has anyone found a way to open a file exclusively where it will fail if the file is already open, i have tried the following _FileStream = new FileStream(@"C:\Data.txt", FileMode.Open,...
6
by: MLH | last post by:
Did A97, perchance, do away with the need for an AutoExec macro to initiate an autoexec sequence for databases? Or, must we still suffer with that hideous macro?
3
by: Hank | last post by:
I'm using Access 2000 and like many of you, I use the AutoExec macro to open my main form. The problem is that for the first few seconds, no form is visible and the user is looking at the list of...
28
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr();...
6
by: darren via AccessMonster.com | last post by:
Hi I have multiple .mde FE's linked to an .mdb BE. The FE is distributed with an Auto FE updater and as such all users are on the same version of the FE. However, one user is getting "Action...
10
by: Tim Streater | last post by:
I have a form and a button to submit it. The button is made from: <input type=button onclick='myHandler(this.form);'> This all works fine except that in Safari 2.0.4, the enter/return keys, if...
6
by: lyle fairfield | last post by:
"dpcman01" <bob@dpcman.com.auwrote in news:48e4a87b$0$31804$afc38c87@news.optusnet.com.au: Much of the code at the MVP site gave excellent results last century. If I were opening a report in...
8
by: Rex the Strange | last post by:
Hello All, I don't know if this is possible, but I'll give it a shot, here. I'm sick of writing the following line of code in control event handlers: if not visible then exit sub Is it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.