Reputation: 71
Here is the code I use to connect my MS Access app to QB Pro 2017 using QBFC:
Dim smgr As QBSessionManage
Set smgr = New QBSessionManager
smgr.OpenConnection "", "Job Management"
smgr.BeginSession "", omDontCare
This works great unless QB isn't open. I need a way to test whether the connection was successful or not. I can't seem to find a code example on how to do this.
Thanks, TD
Upvotes: 0
Views: 191
Reputation: 638
The problem with QBFC is that it doesn't give you an option to check if it's open or if the company file is the same you are trying to open.
If you are okay with just forcing the current QB company file to close, then the following code should do the job.
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
Upvotes: 0