1

我正在创建一个 vb.net 程序,我有一个按钮,单击该按钮将浏览 MDB 文件(代码 1),选择后将执行一些代码行,将 access 数据库中的所有宏填充到一个组合中框(代码 2)。我遇到的问题是代码 2 运行后 MSACCESS.EXE 进程没有关闭。我尝试了一些不同的东西,比如 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
4

2 回答 2

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
于 2013-04-15T18:07:34.867 回答
2

尝试objAccess.Quit在你之后添加一个语句objAccess.CloseCurrentDatabase()

于 2013-04-15T18:08:10.067 回答