【问题标题】:Querying Access From Excel. Expected Query Name After Execute从 Excel 查询访问权限。执行后的预期查询名称
【发布时间】:2020-07-17 10:35:45
【问题描述】:

我正在尝试查询 Access 数据库并根据我的电子表格中的内容返回值。

基于Running an Access Query from Excel,这是我所拥有的:

Sub testdb()

Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter
Dim rs As ADODB.Recordset

Set con = New ADODB.Connection
Set cmd = New ADODB.Command

With con
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .Open "D:\Users\*****\Documents\Database2.accdb"
End With

With cmd
    .ActiveConnection = con
    .CommandText = "SELECT qx FROM Table1 WHERE ID = [MyID]; "
    .CommandType = adCmdStoredProc

    .Parameters.Append cmd.CreateParameter("MyID", adChar, adParamInput, Size:=14)
    .Parameters("MyID") = "ANBMaleNS21216"
End With

Set rs = New ADODB.Recordset
rs.Open cmd

Do Until rs.EOF
    Debug.Print rs.Fields("ID").Value
    rs.MoveNext
Loop

rs.Close
con.Close

Set cmd = Nothing
Set rs = Nothing
Set prm = Nothing
Set con = Nothing

End Sub

当它到达rs.Open cmd 行时会出错

“运行时错误'-2147217900 (80040e14)':
执行后的预期查询名称。

【问题讨论】:

  • 您已将 CommandText 设置为 SQL 语句。正如错误所述,它需要一个查询名称 - Access 中内置的查询对象。引用的链接示例引用了一个名为 Query1 的查询对象,该查询具有所示的 SQL 结构。
  • 您真正想对记录集数据做什么? - 当然不仅仅是输出到即时窗口。
  • 我明白了。那讲得通。我的 VBA 有点生疏了,而且我之前对 Access 做的不多。理想情况下,我想让宏在我的 Access 数据库中的电子表格中查找值列表并返回关联的值。有点像 VLOOKUP,但表数组将存储在 Access 中。
  • 您期望返回什么 - 一条记录?您只想提取一个相关值吗?
  • .CommandType = adCmdText 例如见stackoverflow.com/questions/10352211/…。还要确保添加正确类型的参数。如果 ID 是数字,则不要添加字符类型参数。

标签: excel vba ms-access


【解决方案1】:

如果您要查找的只是一个相关值并且不需要完整的数据集,请考虑:

Dim acc As Access.Application, varData As Variant
Set acc = CreateObject("Access.Application")
acc.OpenCurrentDatabase ("D:\Users\*****\Documents\Database2.accdb")
'code here to start worksheet loop
varData = acc.DLookup("qx", "Table1", "ID=" & need worksheet cell reference here)
Debug.Print IIf(IsNull(varData), "Not found", varData)
'end worksheet loop here

或者也许更好的是只点击一次访问文件以将所有数据拉入记录集,然后使用记录集查找方法。

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Users\*****\Documents\Database2.accdb"
rs.Open "SELECT qx FROM Table1", cn, adOpenDynamic, adLockPessimistic
'code here to start worksheet loop
rs.MoveFirst
rs.Find "ID=" & need worksheet cell reference here
Debug.Print IIf(rs.EOF, "Not found", rs!qx)
'end worksheet loop here

第二种方法实际上可能是更快的过程。

【讨论】:

    【解决方案2】:

    有很多方法可以使用 Excel 来控制 MS Access,反之亦然。这是一种从 Access 将数据拉入 Excel 的简单方法(代码在 Excel 中运行)。

    Sub Import()
    ' Declare the QueryTable object
    Dim qt As QueryTable
    ' Set up the SQL Statement
    sqlstring = "Select LastName, FirstName from Employees Where FirstName In " & Range("A1:A10").Value & ""
    ' Set up the connection string, reference an ODBC connection
    connstring = _
    "ODBC;DSN=Northwind;UID=;PWD=;Database=Northwind"
    ' Now implement the connection, run the Query, and add
    ' the results to the spreadsheet starting at row A1
    With ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("A1"), Sql:=sqlstring)
    .Refresh
    End With
    End Sub
    

    注意:设置对“Microsoft ActiveX Data Objects 2.8 Library”的引用

    这是从 Access 将数据导入 Excel 的另一种类似但不同的方法。

    Sub Select_From_Access()
        Dim cn As Object, rs As Object
        Dim intColIndex As Integer
        Dim DBFullName As String
        Dim TargetRange As Range
    
        DBFullName = "C:\your_path\Northwind.mdb"
    
        'On Error GoTo Whoa
    
        Application.ScreenUpdating = False
    
        Set TargetRange = Sheets("Select").Range("A1")
    
        Set cn = CreateObject("ADODB.Connection")
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DBFullName & ";"
    
        Set rs = CreateObject("ADODB.Recordset")
        rs.Open "SELECT * FROM [OrderDetails] WHERE [OrderID] = 10248", cn, , , adCmdText
    
        ' Write the field names
        For intColIndex = 0 To rs.Fields.Count - 1
        TargetRange.Offset(1, intColIndex).Value = rs.Fields(intColIndex).Name
        Next
    
        ' Write recordset
        TargetRange.Offset(1, 0).CopyFromRecordset rs
    
        Application.ScreenUpdating = True
        On Error Resume Next
        rs.Close
        Set rs = Nothing
        cn.Close
        Set cn = Nothing
        On Error GoTo 0
        Exit Sub
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多