Solved

C# MSAccess - Already opened database

Posted on 2013-12-12
7
581 Views
Last Modified: 2014-01-20
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
0
Comment
Question by:SeanNij
7 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 39715909
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

0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39717352
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.
0
 
LVL 1

Author Comment

by:SeanNij
ID: 39728796
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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39793874
you didn't show the code, but you do use OpenCurrentDatabase, right?
what about the "exclusive" parameter, you use true there?
0
 
LVL 18

Accepted Solution

by:
Dennis Aries earned 500 total points
ID: 39793886
Using
Microsoft.Office.Interop.Access.Application objApp = (Microsoft.Office.Interop.Access.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Access.Application");

Open in new window

you can connect to an existing instance of Access.
The code throws an exception if no instance has been opened.
With that instance, you can check the Currentdb() to see if a database has been opened (returns null if it doesn't) and if so, what name the database has (to ensure that it is your database).

After that, you can proceed using your forms as you see fit.
0
 
LVL 1

Author Closing Comment

by:SeanNij
ID: 39793908
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!!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now