How can I check if access db is open then activate it using C#?

amr ashraf 1 Reputation point
2022-06-17T08:21:22.293+00:00

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 :

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 :

    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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,198 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 86,491 Reputation points
    2022-06-17T09:17:21.91+00:00

    You must check active instances and Release Access before quitting to avoid remaining processes

    Something like :

                string MyFile = Environment.CurrentDirectory + "\\Baseet.accde";  
                Microsoft.Office.Interop.Access.Application AccApp = null;  
                if (Process.GetProcessesByName("msaccess").Count() > 0)  
                {  
                    AccApp = Marshal.GetActiveObject("Access.Application") as Microsoft.Office.Interop.Access.Application;  
                    if (AccApp != null)  
                    {  
                        AccApp.CloseCurrentDatabase();  
                        AccApp.OpenCurrentDatabase(MyFile, false, "017014a");  
                        AccApp.RunCommand(Microsoft.Office.Interop.Access.AcCommand.acCmdAppMaximize);  
                    }  
                }  
                else  
                {  
                    AccApp = new Microsoft.Office.Interop.Access.Application();  
                    AccApp.Visible = true;  
                    AccApp.OpenCurrentDatabase(MyFile, false, "017014a");  
                    AccApp.RunCommand(Microsoft.Office.Interop.Access.AcCommand.acCmdAppMaximize);  
                }  
    

    And when you have finished :

                AccApp.Quit();  
                Marshal.ReleaseComObject(AccApp);  
                AccApp = null;
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.