如何使用QBFC连接到Quickbooks来确定连接是否成功

问题描述 投票:0回答:1

以下是我使用QBFC将MS Access应用程序连接到QB Pro 2017的代码:

Dim smgr As QBSessionManage

Set smgr = New QBSessionManager
smgr.OpenConnection "", "Job Management"
smgr.BeginSession "", omDontCare

除非QB没有打开,否则这很有用。我需要一种方法来测试连接是否成功。我似乎无法找到有关如何执行此操作的代码示例。

谢谢,TD

access quickbooks qbfc
1个回答
0
投票

QBFC的问题在于它没有为您提供选项来检查它是否已打开,或者公司文件是否与您尝试打开的文件相同。

如果您只是强制关闭当前的QB公司文件,那么以下代码应该可以完成。

On Error Resume Next
Dim smgr As QBSessionManager
Dim oServ As Object
Dim cProc As Variant
Dim oProc As Object
Set oServ = GetObject("winmgmts:")
Set cProc = oServ.ExecQuery("Select * from Win32_Process")

Set smgr = New QBSessionManager
smgr.OpenConnection "", "Job Management"
smgr.BeginSession "", omDontCare

If Err.Number <> 0 Then
    'Kill the current running Quickbooks process
    For Each oProc In cProc
        If oProc.Name = "QBW32.EXE" Then
           oProc.Terminate
        End If
    Next
    'Close the connection and try again
    smgr.CloseConnection
    smgr.OpenConnection "", "Job Management"
    smgr.BeginSession "", omDontCare
End If
© www.soinside.com 2019 - 2024. All rights reserved.