Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# MSAccess - Already opened database

Posted on 2013-12-12
7
639 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 35

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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 143

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

860 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