在Excel / VBA中执行Currentdb.execute的问题

时间:2014-01-28 18:09:01

标签: excel vba ms-access

以下是我正在尝试使用excel / access 2007.我在Access中有一个表。我正在尝试创建一个临时表来存储存储在字符串strSQL_Insert中的查询结果。但是当我尝试执行DoCmd.RunSQL时,我得到一个运行时错误'424'。 “所需对象”。我尝试执行查询本身并将结果导入excel,这似乎工作正常。这就是我所拥有的

Sub GetDataADO()

Dim dbConnection As Object
Dim dbRecordSet  As Object
Dim strSQL As String
Dim dbFileName As String
Dim DestinationSheet As Worksheet
Dim strTable As String
Dim strSQL_Insert As String

strTable = "tblTempTest"

Set dbConnection = CreateObject("ADODB.Connection")
Set dbRecordSet = CreateObject("ADODB.Recordset")

dbFileName = "H:\Unscan.accdb"

dbConnection.Provider = "Microsoft.ACE.OLEDB.12.0;Data Source=" _
& dbFileName & ";Persist Security Info=False;"

Set DestinationSheet = Worksheets("Sheet2")

With dbConnection
     .Open
 End With

strSQL_Insert = "INSERT INTO tblTempTest " _
& "SELECT TOP 10 BranchNumber , COUNT(*) as Total FROM BUDetails " _
& "GROUP BY BranchNumber ORDER BY 2 DESC ;"

CurrentDb.Execute strSQL_Insert

dbRecordSet.Close
dbConnection.Close

Set dbRecordSet = Nothing
Set dbConnection = Nothing
Set DestinationSheet = Nothing


Exit Sub

End Sub

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

如果从Excel中运行此命令,则可能无法实例化CurrentDb。尝试用dbConnection替换CurrentDB,看看是否会产生任何结果。