Solved

C# MSAccess - Already opened database

Posted on 2013-12-12
7
593 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

914 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

23 Experts available now in Live!

Get 1:1 Help Now