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 :
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 :
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#:
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