Resolved How can I check if access db is open then activate it ?

Amr Ashraf

Member
Joined
Apr 5, 2022
Messages
14
Programming Experience
Beginner
Hello guys , I'm struggling with something and I can't figure it out , I'm using this code to open a password protected access database :

C#:
public partial class Start_Baseet : System.Windows.Forms.Form
{
string MyFile = Environment.CurrentDirectory + "\\Baseet.accde";
Microsoft.Office.Interop.Access.Application AccApp = new Microsoft.Office.Interop.Access.Application();

public Start_Baseet()
{
    InitializeComponent();
}
public void OpenDb()
{
 
        AccApp.Visible = true;
        AccApp.OpenCurrentDatabase(MyFile, false, "017014a");
        AccApp.RunCommand(AcCommand.acCmdAppMaximize);
        // AccApp.Activate();


    }
}
      

private void Start_Basset_Load(object sender, EventArgs e)
{
    try
    {
        OpenDb();
    }
    catch
    {
        AccApp.Quit();
        MessageBox.Show("Something is missing", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
    }
    finally
    {
        this.Close();
        System.Windows.Forms.Application.Exit();
        System.Windows.Forms.Application.ExitThread();
        // Process.GetCurrentProcess().CloseMainWindow();
    }


It works fine but have two major problems :
a) If the database is already opened it open a new instance of it.
b) In every time the code been executed it adds a new "MSACCESS.EXE" process to the running processes and eat the free memory up .

I tried this :
C#:
        var prc = Process.GetProcessesByName("MSACCESS.EXE*32");
    //    var prc = Process.GetProcessesByName("Microsoft Access");
        if (prc.Length > 0)
        {
            MessageBox.Show("Access Found");
            SetForegroundWindow(prc[0].MainWindowHandle);
        }
        else
        {
            AccApp.Visible = true;
            AccApp.OpenCurrentDatabase(MyFile, false, "017014a");
            AccApp.RunCommand(AcCommand.acCmdAppMaximize);
            // AccApp.Activate();


        }
    }
    [DllImport("user32.dll")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);


But it do nothing and keep adding new process tho the list .

I need two things , Can I make use of existing MSACCESS process in the processes list instead of opening a new instance of access every time ? if there is no process then open a new instance .

the other thing if the database is already opened just activate it don't open new one .

Any help is deeply appreciated guys , Thanks
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
Read the documentation for finding processes by name. "msaccess.exe" is the name. The "*32" is just how Task Manager marks 32 bit processes.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
Chances are that you'll have to pass in "msaccess". The definite way to find out is to use GetProcesses() to see the list of all process and inspect the ProcessName property of each returned process.
 

Gloops

Well-known member
Joined
Jun 30, 2022
Messages
104
Programming Experience
10+
Hello,
What about initializing a boolean flag when you open your database?
I wonder whether it would not be less resource-consuming than browsing all the processes.
You have to be meticulous about this, and perhaps an error catching is not useless.
 
Top Bottom