We help IT Professionals succeed at work.

Close MS Access database Using VB.Net

keschuster
keschuster asked
on
I want to check whether the Access file is open or not in my current application path. If it is open i have to close that Ms Access database using VB.NET?  It has to check in application running path(Current path)

Check for Open database
If Open
Close Access Db
else
Next step
Comment
Watch Question

SILVER EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
I don't understand the context for the question.  Are you talking about a running version of Access with a connection to a database, or a connection within your own application?

Author

Commented:
There is no connection between my VB.net application and MS Access. But Using My application iam replacing files from my server into current Access file path. If Ms Access file is open it should be closed while replacing...I have one button on window form to close Access database.

I have checked in Processes it is showing like MSAccess.exe in Taskmanager. I was tried with below code. But it is not working.

 Dim p() As System.Diagnostics.Process = Process.GetProcesses
        For Each ps In p
            If ps.ProcessName = "MS Access.exe" Then ps.Kill()
        Next
SILVER EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
You can kill the processes by name, but you have to get the process name correct.  If you start Access, then you can look at the Windows Task Manager "Processes" tab to see what the name is.

There is the Process.GetProcessesByName, which is more efficient, and I believe that the process name for Access is MSACCESS.EXE, and you can confirm that for yourself.

Author

Commented:
Yes the process name is MSACCESS.EXE. I can see only MSACCESS.EXE. i cant find my access db name there in taskmanager.

OR

below code is used for running the file. Like this just i need to close this same file.

System.Diagnostics.Process.Start(System.AppDomain.CurrentDomain.BaseDirectory & "\PtsTools.accdr")
SILVER EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
One approach, if you use Process.Start, is to keep track of the return Process object.

Example:
Dim process As Process = Process.Start(fileName)

Open in new window

Author

Commented:
No..at first i open file while downloading updates just i need to close the same file using a button on windows form. l need to kill the access file.
SILVER EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
If you start a process, it should be in the processes list.  Look at the Task Manager "Processes" tab for the process that you started.