MS Access VBA COM共享添加停止访问关闭

时间:2009-01-20 09:56:43

标签: vb.net ms-access add-in

我在Visual Studio 2008中创建了一个共享加载项并使用了共享加载项 加载项向导和我在VB中的编码。当我想要在Access 2003中运行添加时 检查用户是否打开了数据库,因此我设置了AccessApplication 变量作为OnConnection过程中的应用程序对象然后 在按钮上单击我检查AccessApplication.CurrentDB是否为Nothing 如果没有数据库打开Access将在按钮后正确关闭 点击。 但是如果数据库是打开的,那么我必须在VS调试器中停止Access。

请在下面找到我的OnConnection,OnDisconnection和OnClick程序 非常感谢任何帮助。

Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection
    m_oTestMenu.Delete()
    m_oTestBtn.Delete()

    m_oTestMenu = Nothing
    m_oTestBtn = Nothing

    AccessApplication = Nothing
End Sub

Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
    Dim oCommandBars As Microsoft.Office.Core.CommandBars

    On Error GoTo ErrHandler

    AccessApplication = CType(application, Microsoft.Office.Interop.Access.Application)

    oCommandBars = AccessApplication.CommandBars

    ' Add the menu to the existing menu list
    m_oTestMenu = AddMenu(oCommandBars, "Test", "Test")

    ' Now create menu options
    m_oTestBtn = AddMenuButton(m_oTestMenu, _
        "TestBtn", MsoButtonStyle.msoButtonIconAndCaption, "Test Btn", MsoButtonState.msoButtonUp)

    ' Clean up
    oCommandBars = Nothing
    Exit Sub 

ErrHandler:         oCommandBars =没什么         MSGBOX( “错误”)     结束子

Private Sub m_oTestBtn_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles m_oTestBtn.Click
    Dim AccessDB As dao.Database

    On Error GoTo ErrHandler

    AccessDB = AccessApplication.CurrentDb

    MsgBox("DB Found " & AccessDB.Name)

    'Try To Close Everything
    AccessDB.Close()
    AccessDB = Nothing

    AccessApplication.CurrentDb.Close()
    AccessApplication = Nothing

    Exit Sub

ErrHandler:         MsgBox(“点击错误”)         AccessDB =没什么     结束子

1 个答案:

答案 0 :(得分:2)

找到这篇文章

http://www.xtremevbtalk.com/showthread.php?t=160433

看来我需要在OnDisconnect程序结束时手动进行垃圾收集。

添加了

        GC.Collect()
    GC.WaitForPendingFinalizers()

OnDisconnect函数的末尾Access现在关闭

希望这对其他人有帮助