Saturday, November 6, 2010

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

My post back in October regarding how to close MsAccess with Brute Force (Part 1) left out a few other tricks... I'll admit this is a freaky thing to deal with to say the least so here are a few BONUS functions that can help you deal with shutting down a Microsoft Access 2007 Interop session programmatically:..

The KillMsAccessApps is a function that iterates through all open processes running on your machine and kills the ones that are named MSACCESS... quite effective indeed.


''' <summary>
    ''' Close and destroy all MSACCESS Applications
    ''' </summary>
    ''' <remarks></remarks>
    Sub KillMsAccessApps()
        For Each p As Process In Process.GetProcessesByName("MSACCESS")
            p.Kill()
        Next p
    End Sub

You may also need to minimize a session of Microsoft Access 2007 from time to time. Since I'm in such a good mood today, I'll throw in these three BONUS snips for minimizing an MSACCE.


''' <summary>
    ''' Minimize all MSACCESS Applications
    ''' </summary>
    ''' <remarks></remarks>
    Sub MinimizeMsAccessApps()
        For Each p As Process In Process.GetProcessesByName("MSACCESS")
            ShowWindow(p.MainWindowHandle, SHOW_WINDOW.SW_SHOWMINIMIZED)
        Next p
    End Sub

    ''' <summary>
    ''' For minimizing applications
    ''' </summary>
    ''' <param name="hWnd"></param>
    ''' <param name="nCmdShow"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As SHOW_WINDOW) As Boolean

    ''' <summary>
    ''' Human readable flag enums for minimizing applications
    ''' </summary>
    ''' <remarks></remarks>
    <Flags()> _
    Private Enum SHOW_WINDOW As Integer
        SW_HIDE = 0
        SW_SHOWNORMAL = 1
        SW_NORMAL = 1
        SW_SHOWMINIMIZED = 2
        SW_SHOWMAXIMIZED = 3
        SW_MAXIMIZE = 3
        SW_SHOWNOACTIVATE = 4
        SW_SHOW = 5
        SW_MINIMIZE = 6
        SW_SHOWMINNOACTIVE = 7
        SW_SHOWNA = 8
        SW_RESTORE = 9
        SW_SHOWDEFAULT = 10
        SW_FORCEMINIMIZE = 11
        SW_MAX = 11
    End Enum

2 comments:

  1. Hey thanks for the follow up. I was doing something similar in just ending all of the Access processes but was worried about someone having Access open working on other things and then having that process closed. As a result, I decided to go with ADO.net using ACE12 vs the DAO with the interop session. So far I have had pretty good results, especially with the ability to use datasets instead of recordsets in Windows Forms (primarily the datagrid views). Thanks for the follow up and the posts so far though, they have been extremely helpful in getting me started.

    ReplyDelete
  2. I ran some tests using the method you describe and the issue you will run into is that your connection to Access from a 64 bit version of Revit will most likely fail. I have had some luck connecting on some attempts but then writing back to the database is not entirely solid. The Interop is the way to get around this.

    ReplyDelete