Link to home
Start Free TrialLog in
Avatar of SeanNij
SeanNijFlag for South Africa

asked on

C# MSAccess - Already opened database

Hi,

I have been breaking my back on this one and just can't seem to figure out how to do this.
What I want to do is open up a database using C# and open up a form in the database with a specific filter that comes from C#.

This I got working perfectly with the code below:

Access.Application oAccess = new Access.Application();
oAccess.DoCmd.OpenForm("contacts", Access.AcFormView.acNormal, moMissing, "ID=2", Access.AcFormOpenDataMode.acFormPropertySettings ,Access.AcWindowMode.acWindowNormal, moMissing);

Open in new window


The problem I am having now is every time I run the code it opens the database again even when it is already open. Causing 2 instances of the same database open.

My question is: How do I check if the database is already opened, then if so, don't open it again, rather give the form a new filter condition and refresh the results.

Is this at all possible?

Thanks in advance
S
Avatar of TheAvenger
TheAvenger
Flag of Switzerland image

Keep the instance of the application as a member variable and before opening Access do this:

if (this.oAccess == null)
{
  // code to open
}
else
{
  // code to change filter
}

Open in new window

Avatar of PatHartman
You also have to close the form.  I don't think the filer will be reapplied if the form is already open since it is used in the form's Load event which only runs once.

If I may ask, why are you opening a form?  If you want data, you should be opening a recordset using either a query or a table.
Avatar of SeanNij

ASKER

Hi Avenger.

I cannot use that logic, because what we want is a user to double click a .exe file.
Then the C# code in the exe file checks if the access database open already. If open close all open forms and reopen a form with specific filter.
So oAccess will never be null

Hi PatHartman,

The reason I need to open a form is because a user needs to click a button on the form to submit a result,

Thanks
you didn't show the code, but you do use OpenCurrentDatabase, right?
what about the "exclusive" parameter, you use true there?
ASKER CERTIFIED SOLUTION
Avatar of Dennis Aries
Dennis Aries
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SeanNij

ASKER

Exactly what I was looking for thanks!
Works like a charm!

Here is my working code:

                try
                {
                    oAccess = (Microsoft.Office.Interop.Access.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Access.Application");
                }
                catch
                {
                    oAccess = new Access.Application();
                }
                try
                {
// This fails if sDBPath is already open.
                    oAccess.OpenCurrentDatabase(sDBPath, false, "");
                }
                catch { }

Open in new window

Thanks!!