【问题标题】:Using Excel VBA to insert multiple values into a connection query使用 Excel VBA 将多个值插入连接查询
【发布时间】:2017-09-28 12:40:28
【问题描述】:

我有一个有效的 Excel 电子表格,它使用 VBA 根据输入到单个单元格中的内容来更改连接查询中的参数。它不能使用 Excel 参数来获取值,因为变量在连接中而不是在查询的 where 部分中。所以我知道这原则上是可行的,至少对于一个数据单元格来说是这样。

我现在需要创建一个新的电子表格,我需要将一系列数据放入查询中。

查询看起来像这样:

选择 * 从表_A 身份证在哪里 ('一种', '乙', 'C')

VBA 从电子表格的列中提取值 A、B 和 C,目前看起来像这样:

Dim ID_Range As Range
Sheets("Data").Select
Set ID_Range = Sheets("Data").Range("A1:A10")
With ActiveWorkbook.Connections("Query from Database_A").ODBCConnection
    .BackgroundQuery = True
    .CommandText = Array( _
    "Select * FROM Table_A A WHERE A.ID in " "(" + ID_Range + ")")
    .CommandType = xlCmdSql
    .Connection = Array(Array( _
    ODBC;Description= ****
    .RefreshOnFileOpen = False
    .SavePassword = False
    .SourceConnectionFile = ""
    .SourceDataFile = ""
    .ServerCredentialsMethod = xlCredentialsMethodIntegrated
    .AlwaysUseConnectionFile = False
End With

希望我在删除公司特定信息时没有删除任何重要代码。
运行时出现错误:下标超出范围

我需要做什么才能让它工作?

【问题讨论】:

  • "Select * FROM Table_A A WHERE ..." Table_A 后面的剩余 A?
  • 看起来很有希望,谢谢!我现在就测试一下。
  • 您的.Connection 使用括号未闭合的数组
  • 您必须使用 ODBC 连接对象吗?或者是否有另一种解决方案也可以快速从您的后端套装中提取数据?

标签: sql vba excel tsql


【解决方案1】:

如果您对 ODBCConnection 对象没有特定需求(并且我看不出它在您的情况下的优势),您可以使用 ADODB 甚至是好的旧 DAO 来非常简单地做到这一点!

' Create a recordset object.Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset

With rsPubs
' Assign the Connection object.
.ActiveConnection = cnPubs
' Extract the required records.
.Open "SELECT * FROM Authors"
' Copy the records into cell A1 on Sheet1.
Sheet1.Range("A1").CopyFromRecordset rsPubs

' Tidy up
.Close
End With

cnPubs.Close
Set rsPubs = Nothing
Set cnPubs = Nothing

(从https://support.microsoft.com/fr-fr/help/306125/how-to-import-data-from-microsoft-sql-server-into-microsoft-excel粘贴的代码示例)

【讨论】:

    【解决方案2】:

    最好的方法是创建一个函数来处理范围

    Function getCommaSeparatedList(Source As Range) As String
        Dim cell As Range
        Dim results As String
        For Each cell In Source
            results = results & "'" & cell.Value & "',"
        Next
        getCommaSeparatedList = Left(results, Len(results) - 1)
    End Function
    

    【讨论】:

    • 感谢 Thomas,这是 User91504 在上一条评论中发送的链接中使用的解决方案的一部分。
    【解决方案3】:

    试试这样。

    Dim ID_Range As Range
    Dim vR() As String
    Dim n As Integer
    Dim strRange  As String
    Sheets("Data").Select
    Set ID_Range = Sheets("Data").Range("A1:A10")
    For Each Rng In ID_Range
        n = n + 1
        ReDim Preserve vR(1 To n)
        vR(n) = "'" & Rng & "'"
    Next Rng
    strRange = Join(vR, ",")
    
    With ActiveWorkbook.Connections("Query from Database_A").ODBCConnection
        .BackgroundQuery = True
        .CommandText = Array("Select * FROM Table_A A WHERE A.ID in (" & strRange & ")")
    

    【讨论】:

      猜你喜欢
      • 2011-03-29
      • 2013-06-17
      • 2015-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 1970-01-01
      • 2021-08-20
      相关资源
      最近更新 更多