繁体   English   中英

如何从 Excel VBA 运行查询

[英]How do I run a query from Excel VBA

以下代码创建一个新的访问数据库,并将两个不同访问数据库中的表复制到新数据库中。 这部分工作没有错误。

问题出在以下行: TMPConnection.Open TMPConnectionString
它通知该文件已在使用中。

Dim strPath As String
Dim objAccess As Object
Dim dbss As Object
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
strPath = Environ("USERPROFILE") & "\Desktop\TMP.accdb"
'DETERMINE IF DB ALREADY EXISTS AND CREATE IF IT DOES NOT
strDb = Dir(strPath)
Set objAccess = CreateObject("Access.Application")
If Len(strDb) <> 0 Then
    fso.DeleteFile strPath
End If
Call objAccess.NewCurrentDatabase(strPath)
Set dbss = objAccess.CurrentDb
'--------------------------------------------------------------------------------------------------
'COPY ACCESS DATABASE TABLES INTO THE NEWLY CREATED TMP DATABASE
objAccess.DoCmd.TransferDatabase acImport, "Microsoft Access", fileNameNEW, acTable, arr1(i), "N"
objAccess.DoCmd.TransferDatabase acImport, "Microsoft Access", fileNameNEW, acTable, arr2(i), "O"
'--------------------------------------------------------------------------------------------------
'CONNECT TO DATABASE
Dim TMPConnection As ADODB.Connection
Dim TMPRecordsetN As ADODB.Recordset
Dim TMPRecordsetO As ADODB.Recordset
Dim TMPQueryN As String
Dim TMPQueryO As String
Set TMPRecordsetN = New ADODB.Recordset
Set TMPRecordsetO = New ADODB.Recordset
Set TMPConnection = New ADODB.Connection
TMPConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & strPath
TMPConnection.Open TMPConnectionString
'--------------------------------------------------------------------------------------------------
TMPQueryN = "Select * from " & "N"
TMPQueryO = "Select * from " & "O"
Set TMPRecordsetN = TMPConnection.Execute(TMPQueryN)
Set TMPRecordsetO = TMPConnection.Execute(TMPQueryO)
'--------------------------------------------------------------------------------------------------
Worksheets("NEW").Range("A1").CopyFromRecordset TMPRecordsetN

我不确定如何使用这个“已经存在”的连接来执行查询Set TMPRecordsetN = TMPConnection.Execute(TMPQueryN) ( TMPConnection )

我的问题是:
如果已经存在到这个新数据库的连接,我如何使用它来运行查询(例如 TMPRecordsetN)

本质上,您以两种不同的方式连接到 MS Access:前端使用 Access COM object,后端使用 ADO。 此外,您正在组合两个数据库 API,DAO 与CurrentDb和 ADO 与ADODB.Connection ,它们都有记录集对象。

考虑通过 Access COM 应用程序和 DAO 使用第一个连接,或者关闭 COM object 并使用 ADO 连接到新数据库。

方法 1:使用 COM 连接和 DAO 运行所有操作

...
Call objAccess.NewCurrentDatabase(strPath)

objAccess.DoCmd.TransferDatabase acImport, "Microsoft Access", fileNameNEW, acTable, arr1(i), "N"
objAccess.DoCmd.TransferDatabase acImport, "Microsoft Access", fileNameNEW, acTable, arr2(i), "O"

' INITIALIZE DAO DATABASE
Set dbss = objAccess.CurrentDb

' OPEN DAO RECORDSETS
TMPQueryN = "SELECT * FROM [N]"
TMPQueryO = "SELECT * FROM [O]"
Set TMPRecordsetN = dbss.OpenRecordset(TMPQueryN)
Set TMPRecordsetO = dbss.OpenRecordset(TMPQueryO)

ThisWorkbook.Worksheets("NEW").Range("A1").CopyFromRecordset TMPRecordsetN

' CLOSE AND RELEASE DAO OBJECTS
TMPRecordsetN.Close: TMPRecordsetO.Close
Set TMPRecordsetN = Nothing: Set TMPRecordsetO = Nothing: Set dbss = Nothing

' CLOSE AND RELEASE COM OBJECT
objAccess.CloseCurrentDatabase
objAccess.Quit

Set objAccess = Nothing

方法二:关闭没有DAO的COM连接,打开ADO连接

...
Call objAccess.NewCurrentDatabase(strPath)

objAccess.DoCmd.TransferDatabase acImport, "Microsoft Access", fileNameNEW, acTable, arr1(i), "N"
objAccess.DoCmd.TransferDatabase acImport, "Microsoft Access", fileNameNEW, acTable, arr2(i), "O"

' CLOSE AND RELEASE COM OBJECT
objAccess.CloseCurrentDatabase()
objAccess.Quit()

Set objAccess = Nothing


' CONNECT TO DATABASE VIA ADO -----------------------------------------------------
Dim TMPConnection As ADODB.Connection
Dim TMPRecordsetN As ADODB.Recordset, TMPRecordsetO As ADODB.Recordset
Dim TMPQueryN As String, TMPQueryO As String

' OPEN CONNECTION
Set TMPConnection = New ADODB.Connection
MPConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & strPath
TMPConnection.Open TMPConnectionString

' OPEN ADO RECORDSETS
Set TMPRecordsetN = New ADODB.Recordset
TMPQueryN = "SELECT * FROM [N]"
TMPRecordsetN.Open TMPQueryN, TMPConnection

Set TMPRecordsetO = New ADODB.Recordset
TMPQueryO = "SELECT * FROM [O]"
TMPRecordsetO.Open TMPQueryO, TMPConnection

ThisWorkbook.Worksheets("NEW").Range("A1").CopyFromRecordset TMPRecordsetN

' CLOSE AND RELEASE ADO OBJECTS
TMPRecordsetO.Close: TMPRecordsetN.Close: TMPConnection.Close
Set TMPRecordsetO = Nothing: Set TMPRecordsetN = Nothing: Set TMPConnection = Nothing

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM