Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 使用access VBA在SQL语句的WHERE子句中使用数组_Ms Access_Vba - Fatal编程技术网

Ms access 使用access VBA在SQL语句的WHERE子句中使用数组

Ms access 使用access VBA在SQL语句的WHERE子句中使用数组,ms-access,vba,Ms Access,Vba,我有一个数组ListBoxContents(),它将包含像“15”、“16”、“25”这样的项目,最多包含10个项目。我试图检索列Bnumber中的数据,其中长度大于6的数据以('15','16','25',…)开头,即listbox中指定的项。并尝试在sql语句的where cluase中查询这些listbox项 表B列编号包含 Bnumber 152 156 1523 16417 AA454 CC654 18A16 1826 18A16 25A76 54A16 54235A68 我的VB

我有一个数组ListBoxContents(),它将包含像“15”、“16”、“25”这样的项目,最多包含10个项目。我试图检索列Bnumber中的数据,其中长度大于6的数据以('15','16','25',…)开头,即listbox中指定的项。并尝试在sql语句的where cluase中查询这些listbox项

表B列编号包含

 Bnumber
152
156
1523
16417
AA454
CC654
18A16
1826
18A16
25A76
54A16
54235A68
我的VBA代码

Private Sub arraywhere()
Dim qry As String 
Dim Size As Integer
Size = Form_Input_From.lstdigits.ListCount - 1
ReDim ListBoxContents(0 To Size) As String
ReDim LContents(0 To 30) As String       
Dim m As Integer    
For m = 0 To Size
    ListBoxContents(m) = Form_Input_From.lstdigits.ItemData(m)
Next m  


For m = 0 To Size
     qry = "SELECT col1,col2,Bnumber " & _
    "FROM table WHERE (Len([table].[Bnumber]))>6) AND (Left
     ([table].[Bnumber],2))=(" & ListBoxContents(m) & ");"
Next m   

Debug.Print qry    

Application.CurrentDb.QueryDefs("[arrayqry]").sql = qry
DoCmd.OpenQuery "[arrayqry]"

End Sub

但是我的WHERE子句只读取最后一个数组项。如何在where子句中指定数组

在for循环的每次迭代中,您都将
qry
设置为一个新语句。相反,您需要根据列表框内容连接一个字符串,该字符串看起来像
(“x”、“y”、“z”)
,并在
中用
替换
=

完成后,设置查询,它将类似于以下内容:

qry = "SELECT col1,col2,Bnumber " & _
"FROM table WHERE (Len([table].[Bnumber]))>6) AND (Left
 ([table].[Bnumber],2)) in (" & commaSeperatedContents & ");"
其中,
CommaseOperatedContents
是一个类似于
(“x”、“y”、“z”)
的字符串,但当然有您的值。

试试这个:

Dim inPart As String

For m = 0 To Size
    inPart = inPart & "'" & ListBoxContents(m) & "',"
Next m
inPart = Left(inPart, Len(inPart) - 1)

qry = "SELECT col1,col2,Bnumber " & _
   "FROM [table] WHERE Len([table].[Bnumber])>6 AND " & _
   "Left([table].[Bnumber],2) In (" & inPart & ");"
Debug.Print qry

CurrentDb.QueryDefs("[arrayqry]").SQL = qry
DoCmd.OpenQuery "arrayqry"
试试像这样的东西

" ...  ([table].[Bnumber],2)) in ('" & Join(ListBoxContents,"','") & "');"

数组中的项目列表实际上似乎来自
Form\u Import\u from\u PMT.lstdights
控件。此控件是否绑定到数据源?如果是这样,您只需使用join子句将
连接到该数据源,该子句指定仅选择值以连接表中的数字开头的
b编号
行:

select col1, col2, Bnumber
from table as t
inner join tblDigits as d
on left(t.Bnumber, 2) = d.Digits
where len(t.Bnumber) > 6
如果控件没有绑定到数据源,那么现在就绑定它(创建一个新表
tblDigits
来保存数字,如上所示),您就可以使用上面的查询了


简而言之,数据绑定就是如何在Access中“在
where
子句中使用数组。”

Tim,据我所知,语法应该是
In('12'、'22'、'33'、'15')
,但是
Join
在('12,22,33,15')中给了我
@simoco-在我的手机上输入,因此没有达到我通常的标准-请参阅编辑