Working in Access 2007 in VBA:
Basically what I have currently works without errors but I no I'm probably violating something within the code (adodb and DAO possibly?), regardless I can't get the connection to end when the code finishes. If I remove the code for the "import," then the connection does start, executes whatever code, then shuts off which is what I'm looking to do but with an import.
My reasoning for doing this is the QODBC accesses a user account that goes into Quickbooks and then extracts the information. Problem is that the "user" basically stays logged in which isn't good because we need access to single user mode and what have you. Here is the code I have so far. Please help!
Private Sub Connect_Click()
On Error GoTo ErrorHandler
'*****************************************************
'Connects the DB to QODBC, imports, and queries the info
'*****************************************************
Dim msg As String
Dim oConnection
Dim sConnectString
Dim dbs As DAO.Database
Dim lngRowsAffected As Long
'Sets connection string
sConnectString = "DSN=Quickbooks Data;OLE DB Services=-2;"
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open sConnectString
Set dbs = CurrentDb
' Import from QODBC
DoCmd.TransferDatabase acImport, "ODBC Database", "ODBC;DSN=QuickBooks Data;DFQ=C:\Users\Public\Documents\Intuit\QuickBooks\Sample Company Files\QuickBooks 2012\sample_manufacturing business.QBW;SERVER=QODBC;OptimizerDBFolder=%AppData%\QODBC Driver for QuickBooks\Optimizer;OptimizerCurrency=Y;OptimizerAllowDirtyReads=D;OptimizerSyncAfterUpdate=Y;SyncFromOtherTables=N;ForceSDKVersion=<default SDK>;LicenseYear=2018", acTable, "SalesOrder", "SalesOrder1"
'Executes a query that appends a table called 'SalesOrder' from a table called 'SalesOrder1'
dbs.Execute "qryAppendSalesOrder", dbFailOnError
'Bypasses warning messages through an execution of query but this grabs the total appended
lngRowsAffected = dbs.RecordsAffected
'Function that logs how many lines were appended. Basically just an activity table
Globals.Logging "Sales Orders Appended: " & lngRowsAffected
'Updates the 'SalesOrder' from 'SalesOrder1'
dbs.Execute "qryUpdateSalesOrder", dbFailOnError
'Deletes the 'SalesOrder1' table that was imported
DoCmd.DeleteObject acTable, "SalesOrder1"
lngRowsAffected = dbs.RecordsAffected
'Closes Connection
oConnection.Close
Set oConnection = Nothing
ErrorHandler:
If Err.Number <> 0 Then
msg = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & "Error Lne: " & Erl & Chr(13) & Err.Description
MsgBox msg, , "Error", Err.HelpFile, Err.HelpContext
End If
End Sub