【问题标题】:Issue executing Currentdb.execute in Excel / VBA在 Excel / VBA 中执行 Currentdb.execute 的问题
【发布时间】:2014-01-29 03:22:04
【问题描述】:

这是我正在尝试使用 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

任何帮助将不胜感激。

【问题讨论】:

  • 欢迎来到 Stack Overflow!请务必阅读选择标签时弹出的有用说明。

标签: excel vba ms-access


【解决方案1】:

如果您在 Excel 中运行它,则 CurrentDb 可能不会被实例化。尝试用 dbConnection 替换 CurrentDB,看看这是否会产生任何结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多