关闭vb.net中的msaccess.exe

时间:2013-04-15 18:02:24

标签: vb.net ms-access

我正在创建一个vb.net程序,我有一个按钮,单击该按钮将浏览MDB文件(代码1),选中后将执行一些代码行,这些代码行将填充访问数据库中的所有宏进入一个组合框(代码2)。我遇到的问题是MSACCESS.EXE进程在代码2运行后没有关闭。我尝试过几个不同的东西,比如objAccess.CloseCurrentDatabase(),但是没有一个能正常工作..对于我做错了什么想法?

代码1

Private Sub CommandDBPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CommandDBPath.Click
    Dim dialog As New OpenFileDialog()
    dialog.Filter = "Access database (*.mdb)|*.mdb"
    If DialogResult.OK = dialog.ShowDialog Then
        TextDBPath.Text = dialog.FileName
    End If
    SelectDatabaseMacro()
End Sub

代码2

Private Sub SelectDatabaseMacro()
    Dim objAccess As Object  '' Access.Application
    Dim i As Long
    Dim path As String
    path = TextDBPath.Text
    objAccess = CreateObject("Access.Application")
    objAccess.OpenCurrentDatabase(path)
    For i = 0 To objAccess.CurrentProject.AllMacros.Count - 1
        TextReportMacro.Items.Add(objAccess.CurrentProject.AllMacros(i).Name)
    Next
    objAccess.CloseCurrentDatabase()
    objAccess = Nothing

End Sub

2 个答案:

答案 0 :(得分:2)

突然杀死这个过程,

For Each p As Process In Process.GetProcesses()
    If p.ProcessName = "MSAccess" Then
        p.Kill()
    End If
Next

或者对于更“优雅”的方法,试试这个,

该进程必须具有Windows界面(窗口)才能工作。

For Each p As Process In Process.GetProcesses()
If p.ProcessName = "MSAccess" Then
    p.CloseMainWindow()
End If
Next

答案 1 :(得分:2)

尝试在objAccess.Quit之后添加objAccess.CloseCurrentDatabase()语句。