Vb.net MS Access VB COM共享加载项停止访问关闭

Vb.net MS Access VB COM共享加载项停止访问关闭,vb.net,ms-access,add-in,Vb.net,Ms Access,Add In,我在VisualStudio2008中创建了一个共享加载项,并使用 外接程序向导和am编码在VB中。在Access 2003中运行外接程序时,我希望 为了检查用户是否打开了数据库,我设置了AccessApplication 变量作为OnConnection过程中的应用程序对象,然后 单击按钮时,我检查AccessApplication.CurrentDB是否为空 如果没有数据库打开,则单击按钮后Access将正确关闭 点击。 但是如果数据库是打开的,那么我必须在VS调试器中停止访问 请在下面找到

我在VisualStudio2008中创建了一个共享加载项,并使用 外接程序向导和am编码在VB中。在Access 2003中运行外接程序时,我希望 为了检查用户是否打开了数据库,我设置了AccessApplication 变量作为OnConnection过程中的应用程序对象,然后 单击按钮时,我检查AccessApplication.CurrentDB是否为空 如果没有数据库打开,则单击按钮后Access将正确关闭 点击。 但是如果数据库是打开的,那么我必须在VS调试器中停止访问

请在下面找到我的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 
错误处理程序: 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
错误处理程序: MsgBox(“单击错误”) AccessDB=无
End Sub发现了这篇文章

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

似乎我需要在OnDisconnect过程结束时手动进行垃圾收集

加上

        GC.Collect()
    GC.WaitForPendingFinalizers()
到OnDisconnect函数的末尾,访问现在关闭

希望这对其他人有帮助

这一行“AccessApplication.CurrentDb.Close()”是在浪费输入,因为Close对当前在Access UI中打开的应用程序没有影响。