gpt4 book ai didi

ms-access - 使用 Access VBA在SQL语句的WHERE子句中使用数组

转载 作者:行者123 更新时间:2023-12-01 16:02:27 25 4
gpt4 key购买 nike

我有一个数组 ListBoxContents(),它将包含“15”、“16”、“25”等项目......最多 10 个项目。我正在尝试检索列 Bnumber 中的数据,其中数据长度 >6 并以('15', '16','25'...) 开头,即 listbox 中指定的那些项目。并尝试在sql语句的where子句中查询这些列表框项目

表列 Bnumber 包含

 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 子句中指定数组?

最佳答案

尝试类似的事情

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

关于ms-access - 使用 Access VBA在SQL语句的WHERE子句中使用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23301904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com