VBA中的SQL WHERE子句:使用array-variable

时间:2016-07-30 13:17:27

标签: sql arrays vba

如何将数组(存储在变量中)插入VBA中SQL语句的WHERE子句?

recordset1.Open "SELECT * FROM [Table] WHERE [NettingSet] = '" & varRecord & "'"

原始字符串是:

recordset1.Open "SELECT Class FROM [BCBS279_NetSet_Basics] WHERE [LoadingID] =" & LId & "And [NettingSet] = '" & varRecord & "'"

我收到此错误:

  

运行时错误'13':类型不匹配

阵列:

Dim varRecord As Variant
varRecord = slide7_netset_info_ID.GetRows(10, , 0)

此数组只包含字符串:

 Example_1, Example_2, Example_3, ..., Example_10

知道怎么解决吗?

1 个答案:

答案 0 :(得分:1)

您希望在SQL中使用IN,然后在VBA中使用“join”构建IN列表:

". . . WHERE [Name] IN ('" & Join(varRecord, "', '") & "')"