在VBA中使用Access 2007:
基本上我目前的工作没有错误,但我不是可能违反代码中的内容(可能是adodb和DAO?),无论我在代码完成时无法终止连接。如果我删除"导入的代码,"然后连接确实开始,执行任何代码,然后关闭这是我想要做的事情,但导入。
我这样做的理由是QODBC访问进入Quickbooks的用户帐户,然后提取信息。问题是"用户"基本上保持登录状态并不好,因为我们需要访问单用户模式以及你拥有什么。这是我到目前为止的代码。请帮忙!
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
答案 0 :(得分:0)
oConnection.Open - 第一个连接打开QuickBooks数据。
DoCmd.TransferDatabase - 第二个连接打开QuickBooks数据。
oConnection.Close - 第一个连接已关闭
当您使用DoCmd时,MS Access将打开与DSN = QuickBooks数据的连接,并且在关闭MS Access应用程序之前不会关闭它。
与QODBC DSN&amp; QuickBooks因此,您无法关闭QuickBooks应用程序。
您可能必须关闭MS Access应用程序才能释放连接。 Manually Close ODBC DSN Connection
或者找到一种方法来关闭通过DoCmd打开的连接[DoCmd.Close() - 不关闭连接]
或使用oConnection将数据从QODBC表传输到MS Access表。读取每一行并将其添加到MS Access表中。
如需更多帮助,请在我们的支持系统页面上创建支持服务单或搜索此问题: