Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# MSAccess - Already opened database

Posted on 2013-12-12
7
Medium Priority
?
818 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
[X]
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
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 39

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

636 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