Thursday, October 14, 2010

Closing the MS Access Session with Brute Force!

It can be tricky to kill a Microsoft Access database session by simply just closing it. The code below demosntrates how to close the application instance and release it entirely from memory using the Marshal technique.

BRUTE FORCE:


''' <summary>
    ''' Close and destroy on exit
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CloseDB() As Boolean
        Try
            ' Close the database
            m_DaoDB.Close()
            ' Quit the application
            m_AccApp.Quit(Access.AcQuitOption.acQuitSaveAll)
            ' Marshal close just in case
            System.Runtime.InteropServices.Marshal.ReleaseComObject(m_AccApp)
            ' Destroy the main variables
            m_AccApp = Nothing
            m_DaoDB = Nothing
        Catch ex As Exception
            ' Close the database
            m_DaoDB.Close()
            ' Quit the application
            m_AccApp.Quit(Access.AcQuitOption.acQuitSaveAll)
            ' Marshal close just in case
            System.Runtime.InteropServices.Marshal.ReleaseComObject(m_AccApp)
            ' Destroy the main variables
            m_AccApp = Nothing
            m_DaoDB = Nothing
        End Try
    End Function

2 comments:

Michael Carrico said...

Hey I appreciate all of the information as I am just starting to develop with the Revit API and Access and the information couldn't be more timely. I am however having some issues still with closing down Access using the above methods. The database seems to close but the program remains open. An Access window is still visible and cannot be closed; after hitting the x the window looks like it is going to close and then reappears. The only way that I can get rid of the window is to kill the process manually from the Task Manager. While the MSACCESS process is still running, I do have the ability to reopen the database in a new ApplicationClass and query but this Application too stays open and cannot be closed. Any help would be greatly appreciated.

Don said...

Hi reeks19,

I see what you mean and there are a few tricks that I left out of that post... so... I added a part 2! Take a look at the latest post entitled:

Closing a Microsoft Access 2007 Session with Yet Even More Brute Force! (part 2)

I added a snip for killing the hanging processes for MSACCESS as well as how to minimize the app...

Post a Comment

Note: Only a member of this blog may post a comment.